Sponsored by Deepsite.site

Guide to Creating an MCP Client-Host-Server Setup

Created By
tyler-jewell8 months ago
Using xAI models w/ SSE MCP servers in Claude Desktop
Content

Guide to Creating an MCP Client-Host-Server Setup

This guide outlines how to create a system with a host ("Claude Desktop"), a client (FastAPI server with OAuth and SQLite CRUD), and an SSE-based MCP server exposing prompts, resources, and tools, as per the Model Context Protocol (MCP) specification. It also includes instructions for connecting your MCP server to Claude Desktop.

Overview of MCP Architecture

MCP defines a Client-Host-Server architecture:

  • Host: Manages multiple client instances, coordinates AI/LLM integration, enforces security, and handles context aggregation.
  • Clients: Created by the host, each maintains a one-to-one connection with a server, handling protocol negotiation and message routing.
  • Servers: Expose resources, tools, and prompts via MCP primitives, operating independently with security constraints.

In this setup:

  • Host ("Claude Desktop"): A desktop application integrating a FastAPI server for user interactions and an MCP client for server communication.
  • Client: The FastAPI server, part of the host, handling user requests and database operations, using the MCP client for server interactions.
  • Server: An SSE-based MCP server providing prompts, resources, and tools.

Design Principles

MCP emphasizes:

  1. Easy Server Build: Servers focus on specific capabilities, with the host handling orchestration.
  2. High Composability: Modular components combine seamlessly via a shared protocol.
  3. Server Isolation: Only necessary context is shared, with the host controlling cross-server interactions.
  4. Progressive Feature Addition: Minimal core protocol with negotiated capabilities for extensibility.

Step 1: Setting Up the MCP Server

The MCP server uses Server-Sent Events (SSE) over HTTP to stream messages, implementing JSON-RPC for MCP primitives.

Requirements

  • Framework: Python with FastAPI for HTTP and SSE support.
  • Primitives:
    • Prompts: User-controlled templates (e.g., slash commands, menu options).
    • Resources: Contextual data like files or database schemas, identified by URIs (RFC 3986).
    • Tools: Executable functions like database queries or API calls.
  • Transport: Streamable HTTP with SSE (MCP Transports).
  • Capabilities: Declare resources, tools, and completions capabilities during initialization.

Implementation

  1. Create the FastAPI Server:
    • Set up a FastAPI application with an SSE endpoint (e.g., /mcp).
    • Use fastapi.responses.StreamingResponse for SSE streaming.
  2. Implement JSON-RPC Methods:
    • Resources (MCP Resources):
      • resources/list: List available resources with URIs and metadata.
      • resources/read: Retrieve resource content by URI.
      • Support subscriptions (resources/subscribe) and notifications (notifications/resources/updated).
    • Tools (MCP Tools):
      • tools/list: List tools with names, descriptions, and input schemas.
      • tools/call: Execute a tool with provided parameters.
      • Support notifications (notifications/tools/list_changed).
    • Prompts: Implement methods like prompts/list or prompts/execute (assumed, as specifics are unclear).
    • Completion API (MCP Completion):
      • completion/complete: Provide autocompletion for prompt arguments or resource URIs.
  3. Handle SSE:
    • Stream JSON-RPC responses and notifications to connected clients.
    • Ensure resumability and redelivery for robust streaming.
  4. Security:
    • Validate tool inputs and sanitize outputs.
    • Implement access controls and rate limiting.
    • Validate Origin headers for HTTP requests.

Example Resource and Tool Definitions

TypeExample
ResourceURI: file://localhost/documents/report.txt, MIME: text/plain
ToolName: query_db, Input Schema: {"type": "string", "query": "SELECT..."}

Step 2: Setting Up the Host ("Claude Desktop")

The host is a desktop application integrating a FastAPI server and an MCP client.

Requirements

  • Environment: Python-based desktop application (e.g., using Tkinter or PyQt for GUI, or run as a console app).
  • Components:
    • FastAPI Server: Handles user requests with OAuth authentication and SQLite CRUD.
    • MCP Client: Connects to the MCP server via HTTP/SSE.
  • Functionality: Coordinate user requests, local data operations, and server interactions.

Implementation

  1. FastAPI Server:
    • Setup:
      • Use FastAPI with fastapi.security.OAuth2AuthorizationCodeBearer for OAuth.
      • Connect to a local SQLite database using SQLAlchemy.
    • Endpoints:
      • CRUD operations (e.g., /items/{id} for create, read, update, delete).
      • MCP-related endpoints (e.g., /tools/list to fetch tools from the server).
    • Security:
      • Require OAuth bearer tokens for all endpoints.
      • Validate tokens against an identity provider.
  2. MCP Client:
    • Setup:
      • Use requests for HTTP POST/GET and sseclient-py for SSE.
      • Connect to the MCP server’s /mcp endpoint.
    • Protocol Handling:
      • Perform capability negotiation (e.g., {"capabilities": {"resources": {}, "tools": {}}}).
      • Send JSON-RPC requests (e.g., {"method": "tools/list", "params": {}}).
      • Listen for SSE events (responses and notifications).
    • Integration:
      • Expose methods for the FastAPI server to call (e.g., client.list_tools()).
  3. Host Logic:
    • Manage the MCP client lifecycle (create, connect, disconnect).
    • Coordinate AI/LLM integration if using Claude (optional, not specified).
    • Handle notifications (e.g., update local state on notifications/resources/updated).

Example FastAPI Endpoint

from fastapi import FastAPI, Depends, HTTPException
from fastapi.security import OAuth2PasswordBearer
import sqlite3

app = FastAPI()
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")

@app.get("/items/{item_id}")
async def read_item(item_id: int, token: str = Depends(oauth2_scheme)):
    conn = sqlite3.connect("local.db")
    cursor = conn.cursor()
    cursor.execute("SELECT * FROM items WHERE id = ?", (item_id,))
    item = cursor.fetchone()
    conn.close()
    if item:
        return {"id": item[0], "name": item[1]}
    raise HTTPException(status_code=404, detail="Item not found")

Step 3: Integrating the Components

  • Data Flow:
    • User sends a request to the FastAPI server (e.g., /tools/call with tool name and parameters).
    • FastAPI server uses the MCP client to send a tools/call request to the MCP server.
    • MCP server processes the request and streams the result via SSE.
    • MCP client receives the result and passes it to the FastAPI server, which returns it to the user.
  • Error Handling:
    • Handle JSON-RPC errors (e.g., -32602 for unknown tool).
    • Implement timeouts and retries for server requests.
  • Security:
    • FastAPI server enforces OAuth authentication.
    • MCP client validates server responses to prevent injection attacks.
    • Host enforces MCP security boundaries, sharing only necessary context.

Example Interaction Flow

StepComponentAction
1. User RequestFastAPI ServerReceives /tools/list request with OAuth token.
2. MCP Client RequestMCP ClientSends tools/list JSON-RPC request to MCP server.
3. Server ResponseMCP ServerStreams tool list via SSE.
4. Client ProcessingMCP ClientReceives and parses tool list, passes to FastAPI server.
5. User ResponseFastAPI ServerReturns tool list to user.

Security and Best Practices

  • FastAPI Server:
    • Use HTTPS for all endpoints.
    • Validate OAuth tokens securely.
    • Sanitize database inputs to prevent SQL injection.
  • MCP Server:
    • Validate tool inputs and rate-limit invocations.
    • Use secure HTTP headers (e.g., Content-Security-Policy).
  • MCP Client:
    • Prompt for user confirmation on sensitive tool calls.
    • Log interactions for auditing.
  • Host:
    • Enforce MCP’s server isolation principle, sharing minimal context.
    • Regularly update capabilities based on server negotiations.

Notes on Prompts

Prompts are user-controlled templates, but the specification lacks detailed methods (e.g., prompts/list). Assume they are handled similarly to resources or tools, with methods for listing and executing. The Completion API supports promptI supports prompt argument autocompletion, suggesting prompts are interactive inputs. Define custom prompt templates based on your use case (e.g., /code_review for code analysis).

Potential Challenges

  • Prompt Implementation: Lack of specific protocol methods may require custom logic.
  • SSE Reliability: Ensure robust handling of connection drops and message redelivery.
  • Claude Integration: If "Claude Desktop" involves the Claude AI model, integrate it with the host’s AI coordination logic (not specified).

Connecting to MCP Servers with Claude Desktop

To connect your MCP server to Claude Desktop, configure the claude_desktop_config.json file to register your server, enabling Claude to access its resources, tools, and prompts.

Step 1: Locate the Configuration File

Find or create the claude_desktop_config.json file in the appropriate directory:

  • macOS: ~/Library/Application Support/Claude/claude_desktop_config.json
  • Windows: C:\Users\<username>\AppData\Roaming\Claude\claude_desktop_config.json (replace <username> with your actual username)

Step 2: Edit the Configuration File

Add your MCP server under the mcpServers key. For example:

{
    "mcpServers": {
        "my-mcp-server": {
            "command": "python",
            "args": [
                "--directory",
                "/absolute/path/to/your/server",
                "run",
                "server.py"
            ]
        }
    }
}
  • Key Fields:
    • "my-mcp-server": A unique name for your server.
    • "command": The executable to run your server (e.g., python, uv).
    • "args": Arguments including the absolute path to your server directory and the entry file (e.g., server.py).
  • Best Practices:
    • Use absolute paths (e.g., /Users/john/server on macOS, C:\\Users\\John\\server on Windows).
    • Ensure the command and arguments match your server’s execution requirements.

Step 3: Save and Restart Claude Desktop

Save the configuration file and restart Claude Desktop to apply the changes.

Step 4: Verify the Connection

In Claude Desktop, look for the hammer icon in the interface, indicating that your server’s tools are available. You can now interact with your server’s resources, tools, or prompts via Claude’s interface.

What MCP Servers Provide

MCP servers enhance Claude’s capabilities by offering:

  • Resources: File-like data (e.g., API responses, file contents) accessible via URIs.
  • Tools: Functions callable by Claude (with user approval), such as database queries or API calls.
  • Prompts: Pre-written templates for specific tasks (e.g., code generation, data analysis).

Troubleshooting

If the server fails to connect:

  • Verify that all paths in claude_desktop_config.json are absolute and correct.
  • Check server logs in:
    • macOS: ~/Library/Application Support/Claude/logs/
    • Windows: C:\Users\<username>\AppData\Roaming\Claude\logs\
  • Run the server command manually to diagnose errors.
  • Consult the MCP Troubleshooting Guide for detailed debugging steps.

Example Configuration for Windows

For a Windows-based MCP server:

{
    "mcpServers": {
        "my-mcp-server": {
            "command": "python",
            "args": [
                "--directory",
                "C:\\Users\\John\\mcp-server",
                "run",
                "server.py"
            ]
        }
    }
}

Additional Resources

Conclusion

This setup creates a modular, secure system leveraging MCP’s architecture. The FastAPI server provides a user-friendly interface, the MCP client enables server communication, and the SSE-based MCP server delivers rich AI context. By connecting to Claude Desktop, you can extend Claude’s capabilities with custom resources, tools, and prompts. Follow MCP’s design principles for maintainability and extensibility.

Recommend Servers
TraeBuild with Free GPT-4.1 & Claude 3.7. Fully MCP-Ready.
Playwright McpPlaywright MCP server
EdgeOne Pages MCPAn MCP service designed for deploying HTML content to EdgeOne Pages and obtaining an accessible public URL.
Zhipu Web SearchZhipu Web Search MCP Server is a search engine specifically designed for large models. It integrates four search engines, allowing users to flexibly compare and switch between them. Building upon the web crawling and ranking capabilities of traditional search engines, it enhances intent recognition capabilities, returning results more suitable for large model processing (such as webpage titles, URLs, summaries, site names, site icons, etc.). This helps AI applications achieve "dynamic knowledge acquisition" and "precise scenario adaptation" capabilities.
AiimagemultistyleA Model Context Protocol (MCP) server for image generation and manipulation using fal.ai's Stable Diffusion model.
BlenderBlenderMCP connects Blender to Claude AI through the Model Context Protocol (MCP), allowing Claude to directly interact with and control Blender. This integration enables prompt assisted 3D modeling, scene creation, and manipulation.
Howtocook Mcp基于Anduin2017 / HowToCook (程序员在家做饭指南)的mcp server,帮你推荐菜谱、规划膳食,解决“今天吃什么“的世纪难题; Based on Anduin2017/HowToCook (Programmer's Guide to Cooking at Home), MCP Server helps you recommend recipes, plan meals, and solve the century old problem of "what to eat today"
DeepChatYour AI Partner on Desktop
Jina AI MCP ToolsA Model Context Protocol (MCP) server that integrates with Jina AI Search Foundation APIs.
Context7Context7 MCP Server -- Up-to-date code documentation for LLMs and AI code editors
CursorThe AI Code Editor
Tavily Mcp
WindsurfThe new purpose-built IDE to harness magic
Visual Studio Code - Open Source ("Code - OSS")Visual Studio Code
MCP AdvisorMCP Advisor & Installation - Use the right MCP server for your needs
TimeA Model Context Protocol server that provides time and timezone conversion capabilities. This server enables LLMs to get current time information and perform timezone conversions using IANA timezone names, with automatic system timezone detection.
ChatWiseThe second fastest AI chatbot™
Amap Maps高德地图官方 MCP Server
Baidu Map百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。
Serper MCP ServerA Serper MCP Server
MiniMax MCPOfficial MiniMax Model Context Protocol (MCP) server that enables interaction with powerful Text to Speech, image generation and video generation APIs.