Skip to main content
Glama

MCP 服务器实现

基于 Flask 的完整模型上下文协议 (MCP) 实现,用于通过外部工具增强大型语言模型功能。

概述

此存储库演示了如何构建一个处理模型上下文协议 (MCP) 的服务器。MCP 是一种通过直接在模型的文本输出中调用工具来扩展 LLM 功能的方法。与函数调用不同,MCP 将工具定义直接放置在上下文窗口中,并解析模型的自然语言响应以识别工具的使用情况。

特征

  • 🔧完整的 MCP 实现:完整的解析、执行和响应处理

  • 🌤️示例工具:带有参数验证的天气和计算器工具

  • 🔄对话流:在多个交互中保持上下文

  • 🧩基于正则表达式的解析:用于工具调用的灵活文本解析

  • 🚀 Flask API :用于聊天集成的 REST API 端点

项目结构

mcp_server/ ├── app.py # Main Flask application ├── mcp_handler.py # MCP parsing and execution ├── mcp_example.py # Standalone MCP example ├── requirements.txt # Dependencies ├── tools/ # Tool implementations │ ├── __init__.py │ ├── weather.py # Weather API tool │ └── calculator.py # Calculator tool └── README.md # This file

安装

  1. 克隆存储库:

    git clone https://github.com/yourusername/mcp-server.git cd mcp-server
  2. 创建虚拟环境:

    python -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate
  3. 安装依赖项:

    pip install -r requirements.txt
  4. 设置环境变量:

    # Create a .env file with: LLM_API_KEY=your_llm_api_key_here WEATHER_API_KEY=your_weather_api_key_here FLASK_APP=app.py FLASK_ENV=development

用法

运行服务器

启动Flask开发服务器:

flask run

对于生产:

gunicorn app:app

API 端点

  • POST /chat :使用 MCP 处理聊天消息

    curl -X POST http://localhost:5000/chat \ -H "Content-Type: application/json" \ -d '{ "messages": [ { "role": "user", "content": "What's the weather like in Boston?" } ] }'

独立示例

运行示例脚本来查看 MCP 的运行情况:

python mcp_example.py

工作原理

  1. 工具注册:工具及其参数和执行逻辑进行注册

  2. 工具定义注入:将 XML 格式的工具描述添加到提示中

  3. LLM 响应处理:正则表达式模式识别 LLM 文本输出中的工具调用

  4. 工具执行:参数被解析并传递给适当的工具处理程序

  5. 结果注入:工具执行结果被插入到响应中

MCP 与函数调用

特征

微胶囊钙

函数调用

定义位置

在提示文本中

在 API 参数中

调用格式

自然语言

结构化 JSON

执行

文本解析

API 集成

能见度

可见响应

可能被隐藏

平台支持

任何基于文本的法学硕士

需要 API 支持

对话示例

用户:波士顿的天气怎么样?

法学硕士

I'll check the weather for you. get_weather(location="Boston, MA", unit="fahrenheit")

处理后

I'll check the weather for you. get_weather(location="Boston, MA", unit="fahrenheit") Result from get_weather: { "location": "Boston, MA", "temperature": 72, "unit": "fahrenheit", "conditions": "Partly Cloudy", "humidity": 68, "wind_speed": 5.8 }

添加您自己的工具

  1. 创建一个继承自Tool的新类

  2. 定义参数和执行逻辑

  3. 向 MCP 处理程序注册

例子:

class MyTool(Tool): def __init__(self): parameters = [ { "name": "param1", "type": "string", "description": "Description of param1", "required": True } ] super().__init__( name="my_tool", description="Description of my tool", parameters=parameters ) def execute(self, param1): # Tool logic here return {"result": "Processed " + param1}

MCP 配置和调用流程

  1. 工具注册

    • MCP 工具已向处理程序注册

    • 每个工具都提供其名称、描述和参数定义

  2. 工具定义注入

    • 工具定义已添加到系统消息中

    • 格式遵循 MCP 的 XML 结构

  3. LLM 响应处理

    • LLM 生成可能包含工具调用的响应

    • 模式匹配识别文本中的工具调用

    • 工具参数被解析并传递给工具执行方法

  4. 工具执行

    • 使用提供的参数执行工具

    • 结果被重新注入到对话中

  5. 对话管理

    • 已处理且包含工具结果的回复将添加到对话历史记录中

    • 未来的 LLM 请求将包括此历史作为背景

对话示例

以下是对话的示例:

用户:波士顿的天气怎么样?

系统向 LLM 发送带有 MCP 工具定义的提示

LLM 回复

I'll check the weather for you. get_weather(location="Boston, MA", unit="fahrenheit")

MCP Handler解析响应,找到工具调用,并执行天气工具

工具执行结果

Result from get_weather: { "location": "Boston, MA", "temperature": 72, "unit": "fahrenheit", "conditions": "Partly Cloudy", "humidity": 68, "wind_speed": 5.8 }

已处理的响应(发送回用户):

I'll check the weather for you. get_weather(location="Boston, MA", unit="fahrenheit") Result from get_weather: { "location": "Boston, MA", "temperature": 72, "unit": "fahrenheit", "conditions": "Partly Cloudy", "humidity": 68, "wind_speed": 5.8 }

用户:你能计算 144 的平方根吗?

LLM 回复

I can calculate that for you. calculator(expression="sqrt(144)")

MCP Handler解析响应,执行计算器工具

工具执行结果

Result from calculator: { "expression": "sqrt(144)", "result": 12.0 }

已处理的响应(发送回用户):

I can calculate that for you. calculator(expression="sqrt(144)") Result from calculator: { "expression": "sqrt(144)", "result": 12.0 } The square root of 144 is 12.

这演示了 MCP 工具使用的完整流程,从 LLM 的基于文本的调用到执行和响应处理。

执照

麻省理工学院

贡献

欢迎贡献代码!欢迎提交 Pull 请求。

-
security - not tested
-
license - not tested
-
quality - not tested

Related MCP Servers

  • A
    security
    -
    license
    A
    quality
    A Model Context Protocol server that provides basic mathematical and statistical functions to LLMs, enabling them to perform accurate numerical calculations through a simple API.
    Last updated -
    13
    11
    39
    MIT License
  • -
    security
    A
    license
    -
    quality
    A comprehensive toolkit that enhances LLM capabilities through the Model Context Protocol, allowing LLMs to interact with external services including command-line operations, file management, Figma integration, and audio processing.
    Last updated -
    25
    Apache 2.0
    • Linux
    • Apple
  • -
    security
    -
    license
    -
    quality
    A Model Context Protocol server that enables LLMs to interact with databases (currently MongoDB) through natural language, supporting operations like querying, inserting, deleting documents, and running aggregation pipelines.
    Last updated -
    MIT License
    • Apple
  • A
    security
    -
    license
    A
    quality
    A Model Context Protocol server that allows LLMs to interact with Python environments, enabling code execution, file operations, package management, and development workflows.
    Last updated -
    9

View all related MCP servers

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/yisu201506/mcp_server'

If you have feedback or need assistance with the MCP directory API, please join our Discord server