FAST MCP风格Python程序示例
此存储库包含Python中使用FAST MCP协议(二进制,长度前缀JSON)的“模型上下文协议”程序的最小示例。
文件夹:
mcp_server.py--TCP服务器使用FAST MCP(4字节长度前缀,然后是UTF-8 JSON)。client.py--客户端使用FAST MCP发送请求并打印响应。tests/test_mcp.py--服务器处理程序的单元测试。requirements.txt--不需要外部依赖。
FAST MCP协议:
- 每条消息都是一个4字节的大端序长度前缀,后面是UTF-8 JSON有效载荷。
- 请求:
{ "id": , "type": "request", "method": "echo"|"add"|"subtract"|"time"|"generate_architecture", "params": {...} } - 答复:
{ "id": , "type": "response", "result": {...} }或错误"error": {"message":...}
示例方法:
echo:返回中的消息result.echo.add:返回中的总和result.sum.subtract:返回差值result.difference.time:返回当前UTC时间result.time.generate_architecture:返回Mermaid JS图result.mermaid(需要Azure OpenAI凭据)。
Azure OpenAI设置
要使用 generate_architecture 方法,在启动服务器之前设置这些环境变量:
AZURE_OPENAI_API_KEY:您的Azure OpenAI API密钥AZURE_OPENAI_ENDPOINT:您的Azure OpenAI端点(例如https://.openai.aluent.com/)AZURE_OPENAI_DEPLOYMENT:您的部署名称(例如“gpt-4”)
安装依赖项:
pip install -r requirements.txt示例:生成Azure体系结构图
客户端发送如下请求:
{
"id": 4,
"type": "request",
"method": "generate_architecture",
"params": {
"prompt": "Design a scalable web application on Azure with a load balancer, app service, and Azure SQL database."
}
}服务器响应如下:
{
"id": 4,
"type": "response",
"result": {
"mermaid": "graph TD\n..."
}
}用法(PowerShell):
# Start the server (foreground)
python .\mcp_server.py 127.0.0.1 5000
# In another terminal, run the client
python .\client.py 127.0.0.1 5000运行测试:
python -m unittest discover -v笔记:
- 这只是一个例子——该协议故意简单地演示了FAST MCP模式。
\=======
ArchGenerator
这是一个MCP服务器,它将从英文提示符生成azure架构
