Skip to content

Model Context Protocol (MCP)

MCP is an open protocol that standardizes how applications provide context to LLMs. Think of MCP like a USB-C port for AI applications—just as USB-C provides a universal way to connect devices to peripherals, MCP provides a universal way to connect AI models to data sources and tools.

In this framework, MCP enables AI agents to securely query your database models through a read-only interface. It exposes registered models as queryable tools while enforcing proper authentication and authorization.

Use Cases

MCP vs Injecting Data in System Prompt

Injecting data means doing all the queries then add the result in string to the system prompt. While it can provide an answer, there are benefits of using MCP:

MCP Provides More Accurate Result

Without MCP (Injecting in prompt):

System prompt: [10,000 lines of customer data]
               [50,000 lines of order data]
               [5,000 lines of product data]

User: "Show me Q4 sales for Product X"
AI Agent: *searches through massive prompt* "Here's the data… [WRONG DATA]"

With MCP:

User: "Show me Q4 sales for Product X"
AI Agent: [queries only Q4 data for Product X]
AI Agent: "Here are 15 relevant orders..."

Context Window Limit

Without MCP (Injecting in prompt):

You are an assistant.

Below are your information:
- [order 1 data]
- [order 2 data]
- [order 1,000,000 data]

User: "What's John's current order status?"
AI Agent: [queries the whole thing].
AI Agent: "Sorry I'm out of context window."

With MCP

User: "What's John's current order status?"
Claude: [queries in-process MCP server RIGHT NOW]
Claude: "John's order #456 shipped 10 minutes ago"

MCP Integration

Auto-discover Models

Models that inherit from BaseModel are automatically registered as MCP tools, no additional configuration required. For example:

from grit.db.models import BaseModel

class Account(BaseModel):
    

Note: These models use a scoped Manager that automatically enforces permissions, ensuring the AI agent only accesses data the user is authorized to see.

Manual Registration

For models that require custom permission logic beyond the default scoping, you can manually register a toolset with a custom get_queryset method.

from core_agent.mcp_server import mcp_registry


@mcp_registry.register(CustomAgent)
class AgentQueryTool(ModelQueryToolset):
    model = CustomAgent

    def get_queryset(self):
        """
        Filter agents based on user access:
        - Public agents (account=null)
        - Private agents belonging to the user's account
        - User's owned agents
        """
        user = self.request.user

        if user.is_anonymous:
            return super().get_queryset().filter(account__isnull=True)

        if user.is_superuser:
            return super().get_queryset()

        query = Q(account__isnull=True) | Q(owner=user)
        # ... additional permission logic
        return super().get_queryset().filter(query).distinct()

Using MCP from AI Agent

# In agent's prompt, the AI can call:
mcp_query(
    model_name="Account",
    operation="search",
    params={
        "query": "enterprise customers",
        "search_fields": ["name", "description"],
        "limit": 10
    }
)