flask-mcp-server
Flask-based Model Context Protocol (MCP) server for Python. Drop it into any Flask app or run it standalone.
README
flask-mcp-server
Flask-based Model Context Protocol (MCP) server for Python. Drop it into any Flask app or run it standalone. Ships with security and ops features out of the box.
Spec target: MCP 2025-06-18 — Streamable HTTP transport with a unified /mcp endpoint (POST for JSON-RPC; GET for SSE). Back-compat routes retained.
Package version: 0.6.1
Table of Contents
- Requirements
- Install
- Quick Start
- What’s new in 0.6.1
- Configuration (env vars)
- Core Concepts
- Examples
- FAQ
- Troubleshooting
- License
Requirements
- Python 3.9+
- Flask 3.x (auto-installed)
Install
From PyPI
pip install flask-mcp-server
Add to requirements.txt from a file path:
flask-mcp-server==0.6.1
Quick Start
Integrated into an existing Flask app (integrated HTTP):
from flask import Flask
from flask_mcp_server import mount_mcp, Mcp
from flask_mcp_server.http_integrated import mw_auth, mw_ratelimit, mw_cors
app = Flask(__name__)
@Mcp.tool(name="sum")
def sum_(a: int, b: int) -> int:
return a + b
# Mount at /mcp with useful middlewares
mount_mcp(app, url_prefix="/mcp", middlewares=[mw_auth, mw_ratelimit, mw_cors])
if __name__ == "__main__":
app.run(port=8765)
Dedicated server app (ships with docs endpoints):
flask-mcp serve-http
# Swagger: http://127.0.0.1:8765/swagger
# OpenAPI: /docs.json
STDIO transport:
flask-mcp serve-stdio
What’s new in 0.6.1
- Unified MCP endpoint (
/mcp):- POST a single JSON-RPC message (request/notification/response). If the
Acceptheader includestext/event-stream, the server streams the response via SSE; otherwise returns JSON. - GET to open an SSE stream for server messages (optional). Supports
Last-Event-IDfor resumability in future minor releases.
- POST a single JSON-RPC message (request/notification/response). If the
- Protocol header: Accepts
MCP-Protocol-Version: 2025-06-18(or2025-03-26for back-compat); others =>400 Bad Request. - Sessions (minimal): If the POSTed method is
"initialize", response includesMcp-Session-Idheader. Clients can include this header on subsequent requests. - Origin validation: Optional
FLASK_MCP_ALLOWED_ORIGINS(comma-separated). If set, only requests with matchingOriginare allowed at the unified MCP endpoint. - Backwards compatibility: legacy endpoints
/mcp/list,/mcp/call,/mcp/batchremain.
Configuration (env vars)
Auth & roles
export FLASK_MCP_AUTH_MODE=apikey # none|apikey|hmac
export FLASK_MCP_API_KEYS="k1,k2"
export FLASK_MCP_API_KEYS_MAP="k1:admin|user;k2:user" # per-key roles
export FLASK_MCP_HMAC_SECRET="supersecret" # if using HMAC
Rate limiting
export FLASK_MCP_RATE_LIMIT=60/m # format: <N>/<s|m|h|d>
export FLASK_MCP_RATE_SCOPE=key # ip|key
CORS, Origins & Logging
export FLASK_MCP_ALLOWED_ORIGINS="http://localhost:3000,https://your.app"
export FLASK_MCP_CORS_ORIGIN="*"
export FLASK_MCP_LOG_FORMAT=json
Providers autoload
export FLASK_MCP_PROVIDERS="my_pkg.mcp_provider:Provider,another.mod:Provider"
Core Concepts
- Registry — stores tools, resources, prompts, completions.
- Decorators —
@tool,@resource,@prompt,@completion_providerregister elements. - Facade —
Mcp.tool(...)etc. (fluent, same params as decorators). - Roles/ACL — per-element role lists checked at call time.
- TTL cache — per-tool/resource memoization via
ttl=int_seconds(memory backend). - Middleware — pluggable pipeline for integrated HTTP routes.
- Service Providers —
register(container, registry)andboot(app, registry)to wire services/routes. - Transports — Streamable HTTP unified endpoint, STDIO, and SSE notifications (hello + registry change).
Examples
A. Unified MCP endpoint usage
# List registry (JSON)
curl -s -X POST http://127.0.0.1:8765/mcp \
-H "Content-Type: application/json" \
-H "MCP-Protocol-Version: 2025-06-18" \
-d '{"jsonrpc":"2.0","id":1,"method":"mcp.list"}' | jq .
# Call a tool over SSE
curl -N -X POST http://127.0.0.1:8765/mcp \
-H "Accept: text/event-stream, application/json" \
-H "Content-Type: application/json" \
-d '{"jsonrpc":"2.0","id":"2","method":"mcp.call","params":{"kind":"tool","name":"sum","args":{"a":5,"b":7}}}'
B. Tools with roles & TTL cache
from flask_mcp_server import Mcp
@Mcp.tool(name="math.add", roles=["user","admin"], ttl=30)
def add(a: int, b: int) -> int:
return a + b
C. Resources & ResourceTemplates
from flask_mcp_server import resource, ResourceTemplate
@resource(name="profile", ttl=15)
def profile(user_id: int) -> dict:
return {"id": user_id, "name": "Alice"}
tpl = ResourceTemplate("https://api.example.com/items/{id}")
url = tpl.expand(id=42) # "https://api.example.com/items/42"
D. Prompts & Completions
from flask_mcp_server import prompt, completion_provider
from typing import List
@prompt(name="greet")
def greet(name: str) -> str:
return f"Write a warm one-line greeting for {name}."
@completion_provider(name="cities")
def cities(prefix: str="") -> List[str]:
base = ["Dhaka","Chittagong","Khulna","Rajshahi"]
return [c for c in base if c.lower().startswith(prefix.lower())]
E. Batch Calls (compat)
curl -s -X POST http://127.0.0.1:8765/mcp/batch \
-H "Content-Type: application/json" \
-d '[
{"kind":"tool","name":"math.add","args":{"a":1,"b":2}},
{"kind":"prompt","name":"greet","args":{"name":"Bashar"}}
]' | jq .
F. SSE (hello + registry events)
# GET /mcp opens an SSE stream and emits a hello event and future registry change events.
curl -N http://127.0.0.1:8765/mcp
G. Auth: API keys & HMAC
export FLASK_MCP_AUTH_MODE=apikey
export FLASK_MCP_API_KEYS="secret123"
curl -s http://127.0.0.1:8765/mcp/list -H "X-API-Key: secret123"
# HMAC
python - <<'PY'
import hmac, hashlib, json, requests
secret = b"supersecret"
body = json.dumps({"jsonrpc":"2.0","id":1,"method":"mcp.list"}).encode()
sig = hmac.new(secret, body, hashlib.sha256).hexdigest()
print(requests.post("http://127.0.0.1:8765/mcp",
headers={"X-Signature":"sha256="+sig,"Content-Type":"application/json"},
data=body).text)
PY
H. Rate limiting
export FLASK_MCP_RATE_LIMIT=100/m
export FLASK_MCP_RATE_SCOPE=key # or 'ip'
I. Service Providers
# my_pkg/mcp_provider.py
from flask_mcp_server import ServiceProvider, Mcp
class Provider(ServiceProvider):
def register(self, container, registry):
@Mcp.tool(name="time.now")
def now() -> str:
import time; return str(int(time.time()))
J. Discovery (auto-register)
# Suppose your tools live in package 'my_tools'
python -c "from flask_mcp_server.discovery import discover_package; discover_package('my_tools')"
K. CLI
flask-mcp serve-http # unified /mcp endpoint
flask-mcp serve-stdio # stdio transport
flask-mcp list # print registry
L. OpenAPI & Swagger
/docs.json— OpenAPI 3.1 JSON/swagger— Swagger UI
FAQ
Is Redis required? No. 0.6.1 uses in-memory backends by default.
Can I mount it into any Flask project? Yes—mount_mcp(app, url_prefix="/mcp").
Does it fully implement all MCP JSON-RPC methods? It implements the transport semantics and common operations (mcp.list, mcp.call, compat endpoints). If you need additional method names or shapes to match a specific client, open an issue or extend in a Service Provider.
Troubleshooting
- 400 unsupported_protocol_version — set
MCP-Protocol-Version: 2025-06-18. - 403 origin_not_allowed — configure
FLASK_MCP_ALLOWED_ORIGINSor remove it. - 401 invalid_api_key / invalid_signature — check headers and env vars.
- 429 rate_limited — adjust
FLASK_MCP_RATE_LIMITor scope.
Contributing
We welcome contributions to flask-mcp-server! Whether it's bug fixes, new features, improved documentation, or ideas—you're invited to collaborate.
Getting Started
- Fork this repository and clone it locally.
- Set up a virtual environment and install the package in editable mode:
python -m venv .venv && source .venv/bin/activate pip install -e .[dev,redis,async] - Run the test suite:
pytest - Create a new branch for your contribution:
git checkout -b my-feature-branch
- Make your changes and commit:
git commit -m "Add: description of the change"
- Push your branch and open a Pull Request (PR) on GitHub.
Contribution Guidelines
- Follow consistent code style (Black, PEP8).
- Write or update tests for any new logic.
- Keep commits focused and descriptive.
- Document any new features in the README if needed.
- Open an issue if you'd like to discuss a large idea before coding.
Feedback & Issues
Use the GitHub Issues page to report bugs, request features, or ask questions.
We appreciate every contribution, big or small. Thank you for helping improve flask-mcp-server!
License
MIT
推荐服务器
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 模型以安全和受控的方式获取实时的网络信息。