模型上下文协议(MCP)服务器和代理
管理AI模型上下文数据的模型上下文协议服务器的简单实现。
设置
- 安装所需的依赖项:
uv venv
uv pip install -r requirements.txt- 运行服务器:
uv run server.py服务器将于启动 localhost:8080.
API终点
商店上下文
curl -X POST http://localhost:8080/mcp/context \
-H "Content-Type: application/json" \
-d '{
"context_id": "example-context",
"data": {
"key": "value",
"metadata": "example"
}
}'获取上下文
curl -X GET http://localhost:8080/mcp/context/example-context删除上下文
curl -X DELETE http://localhost:8080/mcp/context/example-context列出所有工具
curl -X GET http://localhost:8080/mcp/tools运行代理
OPENAI_API_KEY=$OPENAI_API_KEY uv run agent.py 样本输出:
Connected to server
LLM decided to use: calculator
Reasoning: The user is asking for the sum of 5 and 3, which is a basic arithmetic operation suitable for the calculator tool.
Tool 'calculator' executed with result: {'status': 'success', 'result': 8}
Final result: {'status': 'success', 'result': 8}
LLM decided to use: calculator
Reasoning: The user requested to multiply two numbers, so a calculator tool is appropriate to perform this arithmetic operation.
Tool 'calculator' executed with result: {'status': 'success', 'result': 24}
Final result: {'status': 'success', 'result': 24}
Disconnected from server