Flask MCP Plus
模型上下文协议(MCP)的Flask集成——一个使用Flask构建MCP服务器的框架。
 ](https://www.python.org/downloads/) 
概述
Flask MCP Plus提供了一种使用Flask创建模型上下文协议(MCP)服务器的简单直观的方法。它允许 开发人员可以轻松地公开MCP客户端可以使用的工具、资源和提示,从而促进丰富的 语言模型和外部系统之间的相互作用。
特性
- 易于集成:与Flask应用程序无缝集成
- 类型安全:内置对类型提示和Pydantic验证的支持
- 工具支持:定义具有自动模式生成功能的MCP工具
- 资源管理:使用URI模板处理静态和动态资源
- 快速生成:为AI模型创建动态提示
- JSON模式生成:从Python函数自动生成JSON模式
- 流媒体支持:用于大数据的流式HTTP响应
安装
pip install flask-mcp-plus快速开始
这里有一个基本的例子可以让你开始:
from flask import Flask
from flask_mcp_plus import MCP
app = Flask(__name__)
mcp = MCP()
mcp.init_app(app, "my_mcp_server", __name__)
# Define a simple tool
@mcp.tool
def add(a: int, b: int) -> int:
"""Add two numbers together."""
return a + b
# Define a resource
@mcp.resource("http://example.com/data.json")
def get_data():
"""Return some static data."""
return {"message": "Hello, MCP!"}
# Define a prompt
@mcp.prompt
def greeting(name: str) -> str:
"""Create a personalized greeting."""
return f"Hello, {name}!"
if __name__ == "__main__":
app.run(debug=True)在客户端中使用
要将Flask MCP Plus服务器与MCP客户端一起使用,您需要配置客户端以连接到服务器的 终点。\ 配置格式因您使用的特定MCP客户端而异,但通常遵循以下结构:
{
"mcpServers": {
"my_mcp_server": {
"autoApprove": [],
"disabled": false,
"timeout": 60,
"type": "streamableHttp",
"url": "http://127.0.0.1:5000/mcp"
}
}
}核心概念
工具
工具是MCP客户端可以调用的函数。它们可以接受参数并返回结构化结果。
from flask_mcp_plus import Text, Field
from typing import Annotated
@mcp.tool(
name="calculate_sum",
title="Calculate Sum",
description="Add two floating point numbers",
icons=[
{
"src": "https://example.com/calculator-icon.png",
"mimeType": "image/png",
"sizes": ["48x48"]
}
]
)
def add_float(a: Annotated[float, Field(description="The first number")],
b: Annotated[float, Field(description="The second number")]) -> Text:
return Text(text=str(a + b))资源
资源提供可以通过URI获取的数据。Flask MCP Plus支持静态URI和动态URI模板 随着 参数。
@mcp.resource("file://data/config.json")
def get_config():
"""Return static configuration data."""
return {"setting1": "value1", "setting2": "value2"}
# Dynamic resource with path parameters
@mcp.resource("file://data/users/{user_id}.json")
def get_user(user_id: str):
"""Return user data by ID."""
return {"id": user_id, "name": f"User {user_id}"}提示
提示是返回结构化内容以指导AI模型的专门功能。
from flask_mcp_plus import Message, Text
@mcp.prompt
def daily_fact() -> Message:
"""Return a daily fact as a message."""
return Message(
content=Text(text="The capital of France is Paris.")
)高级用法
自定义输出模式
对于返回结构化数据的工具,您可以指定自定义JSON模式:
@mcp.tool(
output_schema={
"type": "object",
"properties": {
"result": {"type": "string"},
"success": {"type": "boolean"}
},
"required": ["result", "success"]
}
)
def api_call():
return {
"result": "Operation completed successfully",
"success": True
}多种内容类型
工具可以在单个响应中返回多种内容类型:
from flask_mcp_plus import Image, Text
@mcp.tool
def multi_data():
return [
Image(data=base64_image_data),
Text(text="Here's an image and some text")
]运行示例
该存储库在 src/example/ 目录:
tools.py-各种工具定义示例resources.py-资源处理示例prompt.py-快速创建示例
举个例子:
cd src/example
python tools.py配置
MCP服务器可以配置各种选项:
mcp = MCP()
mcp.init_app(
app,
name="my-awesome-server",
import_name=__name__,
url_prefix="/mcp", # Endpoint prefix for MCP routes
version="1.0.0"
)发展
先决条件
- Python 3.9+
- 烧瓶3+
- Pydantic 2+
设置
# Clone the repository
https://github.com/maocatooo/flask-mcp-plus
cd flask-mcp-plus
# Install dependencies
uv sync
许可证
此项目根据Apache 2.0许可证获得许可-请参阅 许可证 文件以获取详细信息。
相关项目
支持
如果您遇到任何问题或有疑问,请在 这 .
