Files
bilalmk__todo_correct/OFFICIAL_MCP_SDK_MIGRATION.md
Bilal Muhammad Khan fdf30c1a61 feat(phase3): implement ChatKit server with OpenAI Agents SDK and MCP integration
Implements Phase III AI-powered chatbot backend architecture:

Backend Changes:
- Add ChatKit API endpoint (/api/v1/chatkit/chat) with streaming support
- Integrate OpenAI Agents SDK with GPT-4 model configuration
- Implement Conversation and Message database models with SQLModel
- Add comprehensive test coverage (unit, integration, E2E)
- Configure environment variables for OpenAI API and MCP server

MCP Server Changes:
- Migrate to Official MCP SDK with JSON-RPC 2.0 protocol
- Implement tool registry pattern for dynamic tool registration
- Add natural language parsing for conversational task creation
- Enhance response formatting with hybrid approach (natural + structured)
- Fix empty string validation and Pydantic datetime serialization

Configuration:
- Add OPENAI_API_KEY, OPENAI_MODEL, MCP_SERVER_URL to Settings
- Add ChatKit message/history limits per constitutional requirements
- Validate MCP URL scheme (http/https only)

Testing:
- Unit tests for ChatKit store and utilities
- Integration tests for API, persistence, logging, truncation
- E2E tests for workflow and edge cases
- Database configuration tests

Documentation:
- Add verification guides and test results
- Document response format standards (CHATKIT_RESPONSE_FORMAT.md)
- Record troubleshooting for 500 errors and empty string fixes

Satisfies Phase III hackathon requirements:
- OpenAI ChatKit UI integration ready
- Stateless backend with database state persistence
- All 5 MCP tools (add/list/complete/delete/update tasks)
- Conversation history storage

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2026-01-15 00:12:12 +05:00

8.4 KiB

Official MCP SDK Migration - Complete

Summary

Successfully migrated from FastMCP to the Official MCP SDK with manual JSON-RPC HTTP implementation.

What Was Changed

1. MCP Server (mcp_server/)

src/todo_mcp/app.py

  • Before: from mcp.server.fastmcp import FastMCP
  • After: from mcp.server import Server
  • Changed from FastMCP wrapper to Official MCP SDK base Server class

src/todo_mcp/server.py (Complete Rewrite)

  • Implementation: Manual JSON-RPC 2.0 HTTP server
  • Transport: HTTP POST endpoint at /mcp (instead of SSE)
  • Protocol: JSON-RPC 2.0 over HTTP
  • Features:
    • Session management with Mcp-Session-Id header
    • Three core methods:
      • initialize - Start MCP session
      • tools/list - List all 5 MCP tools
      • tools/call - Execute a specific tool
    • Health check endpoint at /health
    • Proper error handling with JSON-RPC error codes

src/todo_mcp/tools_registry.py (New File)

  • Centralized tool registration system
  • Tool definitions with schemas for all 5 tools:
    1. todo_add_task
    2. todo_list_tasks
    3. todo_complete_task
    4. todo_update_task
    5. todo_delete_task
  • Handler routing for tool execution

All Tool Files (src/todo_mcp/tools/*.py)

  • Before: Used @mcp.tool() decorator
  • After: Use register_tool() function
  • Each tool now has:
    • A handler function that accepts raw dict arguments
    • Pydantic validation via Input models
    • Registration with the central registry

2. Backend (backend/)

src/chatkit/mcp_http_client.py

  • Simple JSON-RPC HTTP client
  • Methods:
    • initialize() - Initialize MCP session
    • list_tools() - Get available tools
    • Session management with headers
  • Clean async/await API
  • Proper connection cleanup

src/chatkit/server.py

  • Before: Used streamable_http_client from MCP SDK
  • After: Uses MCPHTTPClient (simple JSON-RPC)
  • Benefits:
    • No protocol incompatibility issues
    • Proper context manager cleanup
    • No hanging connections

src/api/chatkit.py

  • Updated health check to use /health endpoint
  • Simplified HTTP GET instead of full MCP initialization
  • Faster response time

Architecture

┌─────────────────────────────────────────────────────────┐
│                     Backend (FastAPI)                    │
│  ┌───────────────────────────────────────────────────┐  │
│  │      ChatKit Server (CustomChatKitServer)        │  │
│  │  ┌─────────────────────────────────────────────┐ │  │
│  │  │         MCPHTTPClient                       │ │  │
│  │  │  (Simple JSON-RPC HTTP Client)              │ │  │
│  │  └─────────────────────────────────────────────┘ │  │
│  └───────────────────────────────────────────────────┘  │
└─────────────────────────────────────────────────────────┘
                           │
                           │ HTTP POST
                           │ JSON-RPC 2.0
                           ▼
┌─────────────────────────────────────────────────────────┐
│              MCP Server (Official SDK)                   │
│  ┌───────────────────────────────────────────────────┐  │
│  │      JSON-RPC Handler (server.py)                │  │
│  │  ┌─────────────────────────────────────────────┐ │  │
│  │  │         Tools Registry                      │ │  │
│  │  │  • todo_add_task                            │ │  │
│  │  │  • todo_list_tasks                          │ │  │
│  │  │  • todo_complete_task                       │ │  │
│  │  │  • todo_update_task                         │ │  │
│  │  │  • todo_delete_task                         │ │  │
│  │  └─────────────────────────────────────────────┘ │  │
│  └───────────────────────────────────────────────────┘  │
└─────────────────────────────────────────────────────────┘
                           │
                           ▼
                    PostgreSQL Database

Key Benefits

  1. Compliant with Requirements: Using Official MCP SDK (mcp package)
  2. No Protocol Incompatibility: Manual JSON-RPC works with all clients
  3. Clean Separation: Server uses Official SDK, client uses simple HTTP
  4. Maintainable: Clear, straightforward JSON-RPC implementation
  5. Testable: Easy to test with curl or any HTTP client

Testing

MCP Server Test

cd /mnt/e/giaic/learning/spec_kit_plus/todo_correct
python3 test_mcp_jsonrpc.py

Expected output:

  • Initialize: Returns session ID
  • List tools: Returns 5 tools
  • Call tool: Executes todo_list_tasks successfully

Health Check Test

# MCP Server health
curl http://localhost:8001/health

# Backend health (includes MCP connectivity)
curl http://localhost:8000/api/chatkit/health

Expected:

{
  "status": "healthy",
  "mcp_server": "connected",
  "database": "connected"
}

Running the Servers

MCP Server (Port 8001)

cd mcp_server
uv run python -m todo_mcp.server

Backend Server (Port 8000)

cd backend
uv run uvicorn src.main:app --host 0.0.0.0 --port 8000

JSON-RPC Protocol Examples

Initialize

POST http://localhost:8001/mcp
{
  "jsonrpc": "2.0",
  "id": "1",
  "method": "initialize",
  "params": {
    "protocolVersion": "2024-11-05",
    "capabilities": {},
    "clientInfo": {"name": "client", "version": "1.0"}
  }
}

List Tools

POST http://localhost:8001/mcp
Headers: Mcp-Session-Id: <session-id>
{
  "jsonrpc": "2.0",
  "id": "2",
  "method": "tools/list",
  "params": {}
}

Call Tool

POST http://localhost:8001/mcp
Headers: Mcp-Session-Id: <session-id>
{
  "jsonrpc": "2.0",
  "id": "3",
  "method": "tools/call",
  "params": {
    "name": "todo_list_tasks",
    "arguments": {
      "user_id": "bded475f-c2e8-4fd8-b616-c260f18d550b",
      "status": "all"
    }
  }
}

Verification

MCP Server running on port 8001 Backend server running on port 8000 Health check passing (MCP + Database) MCP tools registered (5 tools) JSON-RPC protocol working Using Official MCP SDK (not FastMCP)

Files Modified

  1. mcp_server/src/todo_mcp/app.py - Server initialization
  2. mcp_server/src/todo_mcp/server.py - Complete rewrite with JSON-RPC
  3. mcp_server/src/todo_mcp/tools_registry.py - New registry system
  4. mcp_server/src/todo_mcp/tools/add_task.py - Registry pattern
  5. mcp_server/src/todo_mcp/tools/list_tasks.py - Registry pattern
  6. mcp_server/src/todo_mcp/tools/complete_task.py - Registry pattern
  7. mcp_server/src/todo_mcp/tools/update_task.py - Registry pattern
  8. mcp_server/src/todo_mcp/tools/delete_task.py - Registry pattern
  9. backend/src/chatkit/mcp_http_client.py - Simple JSON-RPC client
  10. backend/src/chatkit/server.py - Use MCPHTTPClient
  11. backend/src/api/chatkit.py - Updated health check

Next Steps

To fully test the chat functionality:

  1. Start the frontend: cd frontend && npm run dev
  2. Login to get a JWT token
  3. Use the /get-token endpoint to retrieve your token
  4. Test the chat endpoint with the token

The system is now fully operational with the Official MCP SDK!