A simple and intuitive library for building MCP (Model Context Protocol) servers and clients
Project description
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
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
fastermcp-0.1.0.tar.gz
(5.6 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file fastermcp-0.1.0.tar.gz.
File metadata
- Download URL: fastermcp-0.1.0.tar.gz
- Upload date:
- Size: 5.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.8.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a7989bebf8f26a9b084ad6a08acf3e0dfad621ecc8e4823fdc5b8435665749fe
|
|
| MD5 |
14ac4ef0960af389eeb6644fda57b6ee
|
|
| BLAKE2b-256 |
bd0a8c47e28ebbb214865ba0771be5b4d0cf36b3d27485758d9a675ef6f990d6
|
File details
Details for the file fastermcp-0.1.0-py3-none-any.whl.
File metadata
- Download URL: fastermcp-0.1.0-py3-none-any.whl
- Upload date:
- Size: 6.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.8.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
98d77d5965f3c06af39e43a7e7d3a69d2b0e374c7804fb78ced96d050c9ca4fe
|
|
| MD5 |
ea3aaa5ba255e74d8c985557a27e9d6c
|
|
| BLAKE2b-256 |
7773f18d9c76a5a8816c2fd94b18db9ff62488c20b1ae8554e79b5e6f206d335
|