Flask MCP Server Implementation Guide
A Flask-based reference implementation demonstrating how to build an MCP server with stateless HTTP transport, including example tools for generating random numbers and sentences.
README
MCP Server Implementation Guide
A Flask-based implementation of the Model Context Protocol (MCP) server.
What is MCP?
MCP (Model Context Protocol) is an open-source standard for connecting AI applications to external systems. Think of it like a USB-C port for AI applications — it provides a standardized way to connect AI applications (like Cursor, Claude, or ChatGPT) to data sources, tools, and workflows.
┌─────────────────┐ ┌─────────────────┐
│ │ │ │
│ AI Client │◄───────►│ MCP Server │
│ (Cursor) │ MCP │ (Flask App) │
│ │ │ │
└─────────────────┘ └─────────────────┘
Reference: What is MCP? - modelcontextprotocol.io
JSON-RPC 2.0 Protocol
MCP uses JSON-RPC 2.0 for all communication. There are three message types:
Request
{
"jsonrpc": "2.0",
"id": 1,
"method": "method_name",
"params": { ... }
}
Response (Success)
{
"jsonrpc": "2.0",
"id": 1,
"result": { ... }
}
Response (Error)
{
"jsonrpc": "2.0",
"id": 1,
"error": {
"code": -32600,
"message": "Invalid Request"
}
}
Notification (No Response Expected)
{
"jsonrpc": "2.0",
"method": "notifications/initialized"
}
Reference: Messages - MCP Specification
Required JSON-RPC Methods
An MCP server must implement the following methods:
| Method | Type | Description |
|---|---|---|
initialize |
Request | Handshake and capability negotiation |
notifications/initialized |
Notification | Client confirms initialization complete |
tools/list |
Request | List available tools |
tools/call |
Request | Execute a tool |
resources/list |
Request | List available resources |
resources/read |
Request | Read a resource |
prompts/list |
Request | List available prompts |
prompts/get |
Request | Get a prompt template |
Reference: MCP Specification
Method Details
1. initialize
Establishes connection and negotiates capabilities.
Request:
{
"jsonrpc": "2.0",
"id": 1,
"method": "initialize",
"params": {
"protocolVersion": "2025-06-18",
"capabilities": {},
"clientInfo": {
"name": "Cursor",
"version": "2.1.32"
}
}
}
Response:
{
"jsonrpc": "2.0",
"id": 1,
"result": {
"protocolVersion": "2025-06-18",
"capabilities": {
"tools": { "listChanged": false },
"prompts": {},
"resources": {}
},
"serverInfo": {
"name": "mcp-random-tools",
"version": "1.0.0"
}
}
}
2. notifications/initialized
Client notifies server that initialization is complete. No response required (return HTTP 204).
{
"jsonrpc": "2.0",
"method": "notifications/initialized"
}
3. tools/list
Returns available tools with their schemas.
Request:
{
"jsonrpc": "2.0",
"id": 2,
"method": "tools/list"
}
Response:
{
"jsonrpc": "2.0",
"id": 2,
"result": {
"tools": [
{
"name": "random_number",
"description": "Generate a random integer between min and max",
"inputSchema": {
"type": "object",
"properties": {
"min": { "type": "integer", "description": "Minimum value" },
"max": { "type": "integer", "description": "Maximum value" }
},
"required": ["min", "max"]
}
}
]
}
}
4. tools/call
Executes a tool and returns the result.
Request:
{
"jsonrpc": "2.0",
"id": 3,
"method": "tools/call",
"params": {
"name": "random_number",
"arguments": {
"min": 1,
"max": 100
}
}
}
Response: ⚠️ Important: Results must be in content array format with isError field!
{
"jsonrpc": "2.0",
"id": 3,
"result": {
"content": [
{
"type": "text",
"text": "42"
}
],
"isError": false
}
}
The content array can contain multiple items with different types:
text— Plain text resultimage— Base64 encoded image with mimeTyperesource— Reference to a resource URI
The isError field indicates whether the tool execution failed. Even if the HTTP request succeeds (200), the tool itself may have encountered an error.
Reference: MCP Specification 2025-06-18
5. resources/list & prompts/list
Return empty arrays if not implemented:
{
"jsonrpc": "2.0",
"id": 4,
"result": {
"resources": []
}
}
{
"jsonrpc": "2.0",
"id": 5,
"result": {
"prompts": []
}
}
Reference: Tools - MCP Specification
Cursor Integration Flow
Configuration File
Add your MCP server to ~/.cursor/mcp.json:
{
"mcpServers": {
"Local Flask MCP Server": {
"headers": {},
"url": "http://127.0.0.1:5050/"
}
}
}
Communication Flow
┌──────────────────────────────────────────────────────────────────────────────┐
│ CURSOR ←→ MCP SERVER FLOW │
└──────────────────────────────────────────────────────────────────────────────┘
CURSOR MCP SERVER
│ │
│ ┌─────────────────────────────────────────────┐ │
│ │ PHASE 1: INITIALIZATION │ │
│ └─────────────────────────────────────────────┘ │
│ │
│──── POST initialize ──────────────────────────────►│
│ { protocolVersion, clientInfo, capabilities } │
│ │
│◄─── 200 OK ───────────────────────────────────────│
│ { protocolVersion, serverInfo, capabilities } │
│ │
│──── POST notifications/initialized ───────────────►│
│ │
│◄─── 204 No Content ────────────────────────────────│
│ │
│ ┌─────────────────────────────────────────────┐ │
│ │ PHASE 2: DISCOVERY │ │
│ └─────────────────────────────────────────────┘ │
│ │
│──── POST tools/list ──────────────────────────────►│
│◄─── 200 OK { tools: [...] } ───────────────────────│
│ │
│──── POST resources/list ──────────────────────────►│
│◄─── 200 OK { resources: [] } ──────────────────────│
│ │
│──── POST prompts/list ────────────────────────────►│
│◄─── 200 OK { prompts: [] } ────────────────────────│
│ │
│ ┌─────────────────────────────────────────────┐ │
│ │ PHASE 3: TOOL EXECUTION (on demand) │ │
│ └─────────────────────────────────────────────┘ │
│ │
│──── POST tools/call ──────────────────────────────►│
│ { name: "random_number", arguments: {...} } │
│ │
│◄─── 200 OK ────────────────────────────────────────│
│ { content: [...], isError: false } │
│ │
What Happens When Cursor Starts
- Cursor reads
~/.cursor/mcp.jsonto discover configured MCP servers - Sends
initializerequest to each server to establish connection - Server responds with its capabilities (tools, resources, prompts)
- Cursor sends
notifications/initializedto confirm handshake complete - Cursor queries metadata via
tools/list,resources/list,prompts/list - Tools become available for the AI model to invoke during conversations
Transport: Stateless HTTP
This Flask implementation uses Stateless HTTP transport (simple POST requests). This is suitable for:
- Simple tool servers
- Dockerized or remote deployments
- Servers that don't need bidirectional communication
Limitations of Stateless HTTP:
- ❌ No server-initiated notifications (logging, progress updates)
- ❌ No sampling (server asking the AI for information)
- ❌ No real-time bidirectional communication
For full bidirectional features, you would need SSE (Server-Sent Events) or WebSocket transport.
Reference: MCP Transports
Server Architecture
┌─────────────────────────────────────────────────────────────┐
│ MCP SERVER │
├─────────────────────────────────────────────────────────────┤
│ │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ │
│ │ Tools │ │ Resources │ │ Prompts │ │
│ │ │ │ │ │ │ │
│ │ • random_ │ │ • files │ │ • templates │ │
│ │ number │ │ • databases │ │ • workflows │ │
│ │ • random_ │ │ • APIs │ │ │ │
│ │ sentence │ │ │ │ │ │
│ └─────────────┘ └─────────────┘ └─────────────┘ │
│ │ │ │ │
│ └──────────────────┴──────────────────┘ │
│ │ │
│ ┌────────▼────────┐ │
│ │ JSON-RPC 2.0 │ │
│ │ Handler │ │
│ └────────┬────────┘ │
│ │ │
│ ┌────────▼────────┐ │
│ │ HTTP/Flask │ │
│ │ POST / │ │
│ └─────────────────┘ │
│ │
└─────────────────────────────────────────────────────────────┘
Running the Server
# Install dependencies
pip install flask
# Run the server
flask run --port 5050
# Or with debug mode
FLASK_DEBUG=1 flask run --port 5050
Error Handling
Return proper JSON-RPC error responses:
{
"jsonrpc": "2.0",
"id": 1,
"error": {
"code": -32601,
"message": "Method not found"
}
}
Standard error codes:
| Code | Message |
|---|---|
| -32700 | Parse error |
| -32600 | Invalid Request |
| -32601 | Method not found |
| -32602 | Invalid params |
| -32603 | Internal error |
References
推荐服务器
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 模型以安全和受控的方式获取实时的网络信息。