FasterMCP
FasterMCP是一个简单直观的Python库,用于构建MCP(Model Context Protocol)服务器和客户端。它提供了标准化的方式来为LLMs(Large Language Models)提供上下文和工具。
特性
- 简单直观的API
- 异步支持
- 标准化的工具和资源管理
- 灵活的上下文处理
- 基于FastAPI的高性能服务器
安装
pip install fastermcp
快速开始
创建MCP服务器
from fastermcp import MCPServer, Tool
# 创建服务器实例
server = MCPServer()
# 使用装饰器定义工具
@server.protocol.tool(name="calculator", description="A simple calculator")
async def calculator(operation: str, x: float, y: float):
if operation == "+":
return x + y
elif operation == "-":
return x - y
elif operation == "*":
return x * y
elif operation == "/":
return x / y
raise ValueError("Invalid operation")
# 运行服务器
server.run()
使用MCP客户端
import asyncio
from fastermcp import MCPClient, Resource
async def main():
async with MCPClient("http://localhost:8000") as client:
# 注册资源
resource = Resource(
id="math_context",
type="text",
content="This is a math problem solving context",
metadata={"domain": "mathematics"}
)
await client.register_resource(resource)
# 创建上下文
context = await client.create_context(
prompt="Calculate 2 + 2",
tool_names=["calculator"],
resource_ids=["math_context"]
)
# 执行工具
result = await client.execute_tool(
"calculator",
{"operation": "+", "x": 2, "y": 2}
)
print(result) # 输出: 4
asyncio.run(main())
API参考
MCPServer
服务器类,用于处理MCP请求。
__init__(): 初始化服务器实例run(host: str = "0.0.0.0", port: int = 8000): 启动服务器
MCPClient
客户端类,用于与MCP服务器交互。
__init__(server_url: str): 初始化客户端实例register_tool(tool: Tool): 注册新工具register_resource(resource: Resource): 注册新资源create_context(prompt: str, tool_names: Optional[List[str]], resource_ids: Optional[List[str]]): 创建新上下文execute_tool(name: str, parameters: Dict[str, Any]): 执行工具
Tool
工具类,代表LLM可以使用的工具。
name: 工具名称description: 工具描述parameters: 工具参数定义required_params: 必需参数列表
Resource
资源类,代表LLM可以访问的资源。
id: 资源IDtype: 资源类型content: 资源内容metadata: 资源元数据
Context
上下文类,代表LLM交互的上下文。
prompt: 提示文本tools: 可用工具列表resources: 可用资源列表metadata: 上下文元数据
开发指南
工具开发
- 使用装饰器创建工具:
@server.protocol.tool(name="my_tool", description="Tool description")
def my_tool(param1: str, param2: int) -> Any:
# 工具实现
pass
- 手动创建工具:
tool = Tool(
name="my_tool",
description="Tool description",
parameters={
"param1": {"type": "string"},
"param2": {"type": "integer"}
},
required_params=["param1", "param2"]
)
资源管理
# 创建文本资源
text_resource = Resource(
id="text_data",
type="text",
content="Some text content",
metadata={"source": "user"}
)
# 创建JSON资源
json_resource = Resource(
id="json_data",
type="json",
content={"key": "value"},
metadata={"version": "1.0"}
)
测试
运行测试套件:
pip install fastermcp[dev]
pytest
贡献
- Fork 项目
- 创建特性分支 (
git checkout -b feature/amazing-feature) - 提交更改 (
git commit -m 'Add some amazing feature') - 推送到分支 (
git push origin feature/amazing-feature) - 开启Pull Request
许可证
MIT License
Metadata
Release files for fastermcp 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| fastermcp-0.1.0.tar.gz | 5.6 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| fastermcp-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 12.0 kB
Release files / fastermcp-0.1.0.tar.gz
| Download URL | fastermcp-0.1.0.tar.gz |
|---|---|
| Size | 5.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
a7989bebf8f26a9b084ad6a08acf3e0dfad621ecc8e4823fdc5b8435665749fe
|
|
BLAKE2b-256 checksum How to use checksums |
bd0a8c47e28ebbb214865ba0771be5b4d0cf36b3d27485758d9a675ef6f990d6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.8.5
|
Release files / fastermcp-0.1.0-py3-none-any.whl
| Download URL | fastermcp-0.1.0-py3-none-any.whl |
|---|---|
| Size | 6.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
98d77d5965f3c06af39e43a7e7d3a69d2b0e374c7804fb78ced96d050c9ca4fe
|
|
BLAKE2b-256 checksum How to use checksums |
7773f18d9c76a5a8816c2fd94b18db9ff62488c20b1ae8554e79b5e6f206d335
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.8.5
|
Security for fastermcp 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.