MCP Server
A Python backend implementing the Model Context Protocol with Azure OpenAI integration, enabling applications to interact with LLMs through a standardized interface with streaming capabilities.
README
MCP Server - Model Context Protocol Implementation
A comprehensive Python backend implementing the Model Context Protocol (MCP) with JSON-RPC 2.0, Azure OpenAI integration, and Server-Sent Events streaming capabilities.
Features
- Complete MCP Protocol Support: JSON-RPC 2.0 compliant implementation
- Azure OpenAI Integration: Seamless connection to Azure OpenAI services
- Streaming Responses: Real-time streaming via Server-Sent Events (SSE)
- Resource Management: File system resource discovery and access
- Tool Execution: Extensible tool registry with validation
- Authentication: JWT-based authentication system
- Monitoring: Prometheus metrics collection
- Web Interface: Built-in testing and management interface
Architecture
├── app/
│ ├── core/
│ │ ├── config.py # Configuration management
│ │ ├── errors.py # Custom exception classes
│ │ └── logging.py # Structured logging setup
│ ├── protocol/
│ │ ├── enums.py # MCP protocol enumerations
│ │ └── models.py # Pydantic models for MCP
│ ├── services/
│ │ ├── llm.py # Azure OpenAI service
│ │ ├── resources.py # Resource management
│ │ └── tools.py # Tool registry and execution
│ ├── transport/
│ │ └── http.py # HTTP transport layer
│ ├── auth.py # JWT authentication
│ └── metrics.py # Prometheus metrics
├── static/
│ └── app.js # Frontend JavaScript
├── templates/
│ └── index.html # Web interface
├── main.py # Application entry point
└── server.py # Flask app configuration
Installation
- Clone the repository:
git clone <repository-url>
cd mcp-server
- Install dependencies:
pip install -r requirements.txt
- Set up environment variables:
# Required for Azure OpenAI
export OPENAI_API_KEY="your-azure-openai-api-key"
export AZURE_OPENAI_ENDPOINT="https://your-resource.openai.azure.com"
export AZURE_OPENAI_DEPLOYMENT="your-deployment-name"
export AZURE_OPENAI_API_VERSION="2024-08-01-preview"
# Optional configurations
export JWT_SECRET="your-jwt-secret"
export SESSION_SECRET="your-session-secret"
Configuration
The server supports both Azure OpenAI and standard OpenAI configurations:
Azure OpenAI (Recommended)
USE_AZURE_OPENAI = True
AZURE_OPENAI_ENDPOINT = "https://your-resource.openai.azure.com"
AZURE_OPENAI_DEPLOYMENT = "gpt-4o"
AZURE_OPENAI_API_VERSION = "2024-08-01-preview"
Standard OpenAI
USE_AZURE_OPENAI = False
OPENAI_MODEL = "gpt-4o"
Running the Server
Development
python main.py
Production
gunicorn --bind 0.0.0.0:5000 --reuse-port --reload main:app
The server will be available at http://localhost:5000
API Endpoints
MCP Protocol
POST /rpc- JSON-RPC 2.0 endpoint for MCP requestsGET /events- Server-Sent Events for streaming responses
Management
GET /- Web interface for testing and managementGET /health- Health check endpointGET /metrics- Prometheus metrics
Authentication
The server uses JWT-based authentication. Include the token in requests:
# HTTP Headers
Authorization: Bearer <token>
# Query Parameters (for SSE)
?token=<token>
Default development token: devtoken
MCP Protocol Support
Capabilities
- Resources: File system resource discovery and reading
- Tools: Extensible tool execution with validation
- Sampling: LLM completion requests (streaming and non-streaming)
- Logging: Structured JSON logging
Example Requests
Initialize Connection
{
"jsonrpc": "2.0",
"id": "init",
"method": "initialize",
"params": {
"protocolVersion": "2024-11-05",
"capabilities": {},
"clientInfo": {"name": "test-client", "version": "1.0.0"}
}
}
List Resources
{
"jsonrpc": "2.0",
"id": "resources",
"method": "resources/list",
"params": {}
}
Execute Tool
{
"jsonrpc": "2.0",
"id": "tool",
"method": "tools/call",
"params": {
"name": "calculate",
"arguments": {"operation": "add", "a": 5, "b": 3}
}
}
LLM Completion
{
"jsonrpc": "2.0",
"id": "completion",
"method": "sampling/createMessage",
"params": {
"messages": [{"content": {"type": "text", "text": "Hello, world!"}}],
"maxTokens": 100
}
}
Extending the Server
Adding New Tools
from app.services.tools import mcp_tool
@mcp_tool("my_tool", {
"type": "object",
"properties": {
"param1": {"type": "string"},
"param2": {"type": "number"}
},
"required": ["param1"]
})
async def my_custom_tool(param1: str, param2: float = 0.0):
"""Custom tool implementation"""
return {"result": f"Processed {param1} with {param2}"}
Custom Resource Handlers
from app.services.resources import ResourceService
class CustomResourceService(ResourceService):
async def list_resources(self, base_path: str = "."):
# Custom resource discovery logic
pass
Monitoring
The server includes comprehensive monitoring:
- Prometheus Metrics: Request counts, response times, error rates
- Structured Logging: JSON-formatted logs with correlation IDs
- Health Checks: Application and dependency status
Security
- Environment-based configuration (no hardcoded secrets)
- JWT authentication with configurable secrets
- Input validation on all endpoints
- Rate limiting headers from Azure OpenAI
Development
Running Tests
# Test the API endpoints
curl -X POST http://localhost:5000/rpc \
-H "Authorization: Bearer devtoken" \
-H "Content-Type: application/json" \
-d '{"jsonrpc":"2.0","id":"test","method":"initialize","params":{}}'
# Test streaming
curl -N "http://localhost:5000/events?token=devtoken&prompt=Hello&stream=true"
Adding Dependencies
pip install <package-name>
pip freeze > requirements.txt
Troubleshooting
Common Issues
-
Azure OpenAI Connection Errors
- Verify
AZURE_OPENAI_ENDPOINTandAZURE_OPENAI_DEPLOYMENT - Check API key permissions
- Ensure correct API version
- Verify
-
Authentication Failures
- Verify JWT token format
- Check token expiration
- Ensure correct secret configuration
-
Streaming Issues
- Use query parameters for SSE authentication
- Check network connectivity for long-running streams
Debug Logging
Enable debug logging by setting:
export DEBUG=true
License
This project is licensed under the MIT License.
Contributing
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests for new functionality
- Submit a pull request
Support
For issues and questions:
- Check the troubleshooting section
- Review the API documentation
- Open an issue on GitHub
推荐服务器
Baidu Map
百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。
Playwright MCP Server
一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。
Magic Component Platform (MCP)
一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。
Audiense Insights MCP Server
通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。
VeyraX
一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。
graphlit-mcp-server
模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。
Kagi MCP Server
一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。
e2b-mcp-server
使用 MCP 通过 e2b 运行代码。
Neon MCP Server
用于与 Neon 管理 API 和数据库交互的 MCP 服务器
Exa MCP Server
模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。