π¦
AI General MCP Advanced
Advanced practice with MCP (Implementation from scratch)
0 installs
Trust: 39 β Low
Docs
Ask AI about AI General MCP Advanced
Powered by Claude Β· Grounded in docs
I know everything about AI General MCP Advanced. Ask me about installation, configuration, usage, or troubleshooting.
0/500
Loading tools...
Reviews
Documentation
Advanced MCP (Server & Client) Practice Task
Python implementation for building AI Agent with MCP (Model Context Protocol) tools and MCP server/client architecture.
π― Task Overview
Create and run an MCP server with custom tools, then implement an AI Agent with MCP Client that utilizes tools from the created server. This task demonstrates the full MCP workflow from server implementation to client integration.
π Learning Goals
By completing this project, you will learn:
- MCP Protocol Implementation: Understand the Model Context Protocol specification and JSON-RPC communication
- Server-Side Tool Development: Create custom tools that follow MCP standards
- Client Integration: Connect AI agents to MCP servers and handle tool execution
- Session Management: Implement proper session handling and state management
- Streaming Responses: Work with Server-Sent Events (SSE) for real-time communication
- Error Handling: Implement robust error handling in distributed systems
ποΈ Architecture
βββ agent/ # MCP Client Implementation
β βββ clients/
β β βββ custom_mcp_client.py π§ TODO: Pure Python MCP client
β β βββ mcp_client.py β
Complete: Framework-based client
β β βββ openai_client.py β
Complete: AI model integration
β βββ models/
β β βββ message.py β
Complete: Message structures
β βββ app.py π§ TODO: Test it with MCPClient and CustomMCPClient
βββ mcp_server/ # MCP Server Implementation
βββ models/
β βββ request.py β
Complete: Request model
β βββ response.py β
Complete: Response model
βββ services/
β βββ mcp_server.py π§ TODO: Implement core server logic
βββ tools/
β βββ base.py β
Complete: Abstract tool interface
β βββ create_user_tool.py π§ TODO: Implement web search tool
β βββ delete_user_tool.py π§ TODO: Implement web search tool
β βββ update_user_tool.py π§ TODO: Implement web search tool
β βββ get_user_by_id_tool.py π§ TODO: Implement web search tool
β βββ search_users.py π§ TODO: Implement web search tool
βββ server.py π§ TODO: Implement FastAPI server
π Requirements
- Python: 3.11 or higher
- Dependencies: Listed in
requirements.txt - Optional: Postman for API testing
π§ Setup Instructions
- Create virtual environment
python -m venv .venv - Install Dependencies
pip install -r requirements.txt
π Task:
If the task in the main branch is hard for you, then switch to the with-detailed-description branch
Create MCP Server:
- Run docker desctop with UMS
- Open mcp_server and review mcp server structure:
- in models persist implemented request and response models, details about request and response official documentation
- in services/mcp_server.py you need to implement parts described in
TODOsections - in tools you will find simple tools
- lastly, in server.py provide implementations described in
TODOsections
- Run MCP server locally
- Test it with Postman. Import mcp.postman_collection.json into postman. (
init->init-notification->tools/list->tools/call) - Open agent/app.py and run it locally with MCPClient and implement it
- Test agent with queries below π
- Provide implementations described in
TODOsections for custom_mcp_client.py - Test again agent with queries below π
Check if Arkadiy Dobkin present as a user, if not then search info about him in the web and add him
π MCP Protocol Details
JSON-RPC Structure
Request Format:
{
"jsonrpc": "2.0",
"id": "unique-request-id",
"method": "method_name",
"params": {
"parameter": "value"
}
}
Response Format:
{
"jsonrpc": "2.0",
"id": "matching-request-id",
"result": {
"data": "response_data"
}
}
MCP Session Flow
- Initialize: Client sends
initializerequest - Notification: Client sends
notifications/initialized - Discovery: Client calls
tools/listto get available tools - Operation: Client calls
tools/callwith specific tool and arguments - Shutdown:
DELETE, {host}, Mcp-Session-Id: {Mcp-Session-Id}, shutdown is not covered in this practice, but it's simple REST request
Headers
Content-Type:application/jsonAccept:application/json, text/event-streamMcp-Session-Id: Session identifier (after initialization)
π― Implementation Tips
Custom MCP Client Implementation
- Error Handling: Always check for HTTP session initialization
- Session Management: Store and reuse session IDs properly
- SSE Parsing: Look for
data:prefixed lines, ignore[DONE] - JSON-RPC Errors: Check for
errorfield in responses - Content Extraction: Tool results are in
result.content[0].text
Common Issues
- Missing Accept Header: Server requires both JSON and SSE accept types
- Session ID Missing: Most operations require a valid session ID
- Tool Arguments: Arguments must be properly formatted as per tool schema
- Async Context: Use proper async/await patterns for HTTP requests
