Skip to content

en 03 04 model context protocol mcp

Rael Corrales edited this page Nov 17, 2026 · 1 revision

Model Context Protocol (MCP 2.x): Servers, Clients, and Tool Meshes

The Model Context Protocol (MCP) is the open standard initiated by Anthropic to securely connect AI models with external tools, contextual resources, and prompt templates.

ai-rag-common-framework includes full support for MCP 2.x in both Server and Client modes.


1. MCP Architecture Overview

flowchart LR
    subgraph Host ["MCP Client / Host (Claude Desktop, IDE, FrameworkMCPClient)"]
        Consumer["Autonomous Agent / LLM"]
    end

    subgraph Transports ["Supported Transports"]
        Stdio["stdio (Local Unix/Windows Process Pipes)"]
        SSE["SSE (HTTP Server-Sent Events for Distributed Microservices)"]
    end

    subgraph Server ["FrameworkMCPServer"]
        Tools["Tools (Executable functions)"]
        Resources["Resources (Documents, databases, logs)"]
        Prompts["Prompts (Parameterized templates)"]
    end

    Consumer <--> Transports
    Transports <--> Server
Loading

2. Creating an MCP Server with FrameworkMCPServer

Expose Python functions as MCP tools in minutes.

Local Server via stdio (Ideal for Claude Desktop or Terminal Tools):

# server_mcp_stdio.py
from common_framework import create_mcp_server, tool

@tool(name="check_inventory", description="Returns stock levels by SKU.")
def check_inventory(sku: str) -> dict:
    return {"sku": sku, "stock": 42, "warehouse": "Austin-Central"}

server = create_mcp_server(
    name="Enterprise-Inventory-Server",
    version="1.0.0",
    transport="stdio"
)
server.register_tool(check_inventory)

if __name__ == "__main__":
    server.run()

Configuration in Claude Desktop (claude_desktop_config.json):

{
  "mcpServers": {
    "inventory": {
      "command": "python",
      "args": ["C:/projects/server_mcp_stdio.py"]
    }
  }
}

3. Remote MCP Server via HTTP / SSE (Cloud Microservices)

For distributed cloud architectures, the SSE transport allows remote services to consume tools over standard HTTP:

# server_mcp_sse.py
from common_framework import create_mcp_server, tool

@tool(name="run_readonly_sql", description="Executes a safe read-only SQL query.")
def run_sql(query: str) -> list[dict]:
    return [{"id": 1, "user": "alice@corp.com", "role": "Admin"}]

server_sse = create_mcp_server(
    name="Remote-SQL-Server",
    version="1.0.0",
    transport="sse",
    host="0.0.0.0",
    port=8080
)
server_sse.register_tool(run_sql)

if __name__ == "__main__":
    server_sse.run()

4. Dynamic Tool Consumption with FrameworkMCPClient

An agent can connect dynamically to a remote MCP server, discover available tools, and inject them into its tool registry at runtime:

import asyncio
from common_framework import (
    create_mcp_client,
    ReActAgent,
    LLMFactory
)

async def consume_remote_tools():
    # 1. Connect to remote MCP endpoint
    mcp_client = await create_mcp_client("http://localhost:8080/sse")
    
    # 2. Discover exposed tools
    remote_tools = await mcp_client.discover_tools()
    print(f"Discovered Tools: {[t.name for t in remote_tools]}")

    # 3. Mount tools directly into local ReActAgent
    llm = LLMFactory.create("gemini", model="gemini-2.5-flash")
    agent = ReActAgent(
        llm_client=llm,
        tools=remote_tools
    )

    response = await agent.run("List all admin users from the database.")
    print("Agent Response with Remote Tool:\n", response.content)

    await mcp_client.close()

if __name__ == "__main__":
    asyncio.run(consume_remote_tools())

5. Converting a RAG Pipeline into an MCP Tool (as_tool)

Convert any RAGEngine pipeline into an MCP tool with a single call:

from common_framework import RAGEngine, SQLiteVectorStore, EmbeddingFactory, LLMFactory

# 1. Build RAGEngine
llm = LLMFactory.create("gemini")
store = SQLiteVectorStore("manuals.db", embedding_provider=EmbeddingFactory.create("gemini"))
rag = RAGEngine(vector_store=store, llm_client=llm)

# 2. Export as Tool in one line
rag_tool = rag.as_tool(
    name="search_technical_manuals",
    description="Retrieves engineering specs and repair procedures from hardware manuals."
)

# 3. Register on MCP Server
server.register_tool(rag_tool)

English Documentation | Documentación en Español


English Documentation (EN)


Documentación en Español (ES)

Clone this wiki locally