Improving Knowledge Retrieval with MCP and Databases
This article explores a new approach to enhancing the precision of external knowledge retrieval for large language models (LLMs) using the Model Context Protocol (MCP) and databases. It compares this method to Retrieval-Augmented Generation (RAG) and discusses the benefits and limitations of each.
Limitations of RAG for Knowledge Retrieval
RAG is a common technique for connecting LLMs to external knowledge bases, combining information retrieval and generative models. It aims to improve knowledge accuracy, context understanding, and the use of up-to-date information. However, RAG has several limitations:
- Insufficient Retrieval Accuracy: RAG relies on converting knowledge into vectors and matching them with the user's input vector. The accuracy depends on the similarity matching, which can include irrelevant information or miss key details.
- Incomplete Generated Content: RAG processes document fragments, lacking the context of the entire document. This makes it difficult to answer questions that require a holistic understanding.
- Lack of a Holistic View: RAG cannot determine the number of fragments needed or understand the relationships between documents, leading to outdated or incomplete information.
- Weak Multi-Turn Retrieval Capability: RAG struggles with complex reasoning tasks that require multiple rounds of queries and retrieval.
While some techniques like GraphREG and KAG improve RAG, they are still immature, and RAG falls short of expectations.
Introduction to MCP
MCP (Model Context Protocol) is an open standard protocol designed to facilitate interaction between AI models and external data sources or tools. It aims to solve the problem of connecting models to new data sources, which traditionally requires extensive code development.
Function Call and its Drawbacks
Function call, introduced by OpenAI, allows models to interact with external systems, like accessing databases or using external tools. While powerful, it has limitations:
- High Implementation Cost: Models need to support function call, requiring specific training or fine-tuning.
- Inconsistent Standards: Different models implement function call differently, requiring developers to adapt their tools for each model.
MCP as a Standardized Solution
MCP acts as a universal interface, enabling standardized interaction between AI models and external resources, promoting reusability.
- Standardized Protocol: Provides a uniform format for tools.
- Ecosystem Compatibility: Enables tools to work with multiple models and clients with a single development effort.
- Dynamic Extension: Allows adding new tools without modifying the model or client code.
MCP addresses key challenges by facilitating the interaction between local and cloud data sources, reducing redundant development efforts, and creating a unified tool market.
MCP Architecture and Usage
MCP operates using a client-server architecture.
- An MCP client (e.g., Cloud client, Cursor) interacts with an MCP server through a standardized protocol.
- The MCP server, provided by third-party developers, handles the interaction logic with external resources (e.g., databases, browsers, local files).
- The MCP server returns the results to the client through the MCP protocol.
This architecture enables developers to focus on implementing the interaction logic for their tool without worrying about model-specific adaptations.
MCP Client Capabilities
MCP clients offer several capabilities:
- Tools: Executable programs exposed by the server for model interaction.
- Resources: Data and content exposed by the server for client access.
- Promises: Reusable prompt templates for guiding model interaction.
- Sampling: Enables the server to initiate requests to the client for complex intelligent behavior.
- Routes: Mechanism for clients to specify resources for the server to monitor.
Practical Example: MCP with MongoDB
This section demonstrates using MCP with a MongoDB database to improve structured data retrieval.
Setting up MongoDB
MongoDB is a document-oriented database that stores data in JSON format. Its flexible schema is well-suited for building knowledge bases.
- Install the MongoDB Community Server.
- Install MongoDB Compass, a GUI client for managing the database.
- Import the data into MongoDB (e.g., student information, teacher information).
Using Client for MCP
Client is a free, open-source AI coding assistant plugin for VS Code that supports MCP.
- Install Client from the VS Code marketplace.
- Configure the desired model provider (e.g., OpenRouter).
- Find a MongoDB MCP server (e.g.,
mcp-mongo-server
). - Configure the MCP server in Client, specifying the MongoDB connection string.
Testing the MCP Configuration
Once configured, you can test the MCP setup by asking the model questions that require database access. Client will automatically detect the need for MCP and prompt for permission to access the database.
Optimizing Performance with Global Prompts
Models do not inherently understand the structure of the database. Providing the model with the database schema and instructions in the global prompt can improve accuracy and efficiency.
Comparing MCP and RAG with a Student Dataset
Using a student dataset, this section compares the retrieval performance of MCP and RAG. MCP, combined with a database, consistently delivers more accurate and complete results for complex queries compared to RAG.
Limitations and Future of MCP
While promising, MCP has limitations:
- Potential for Large Data Retrieval: MCP can retrieve large amounts of data, potentially consuming many tokens and causing performance issues.
- Reliance on Prompt Engineering: Many MCP clients rely heavily on prompt engineering to communicate with MCP, increasing token consumption.
Despite these limitations, MCP has the potential to become a popular approach for structured data retrieval, particularly in scenarios like customer service, warehouse management, and information management.