MCP主服务器工具包

一个Python工具包,用于创建MCP主服务器,作为客户端与多个MCP服务器之间的中介,支持通过Streamable HTTP传输连接客户端和服务器,并支持通过LangGraph等推理结构选择工具。

  • by ashen-321
  • Python
  • 本地部署
  • 1 星标
  • 更新于
在 GitHub 查看 ashen-321/mcp-master

使用说明

mcp大师

Python包 主MCP服务器.

目录

概述

MCP Master包允许在客户端和MCP服务器之间创建中介。这将与工具相关的推理和逻辑卸载到主服务器,而不需要客户端修改。此Python包提供完整的主MCP支持,使其易于:

  • 构建MCP主服务器,通过Streamable HTTP传输将客户端连接到多个MCP服务器
  • 通过LangGraph等推理结构选择工具
  • 轻松扩展可用的工具选择

阅读模型上下文协议 这里.

安装

对于使用pip的项目:

pip install mcp-master

快速启动

主MCP服务器

一个简单的主MCP服务器,连接到两个附带的演示服务器:

# master_server.py
from src.mcp_master import MasterMCPServer
from src.mcp_master import global_config as gconfig
from os import getenv

gconfig.selector_model_id = ''  # Set this to your tool selector model ID
gconfig.judge_model_id = ''  # Set this to your judge model ID
gconfig.judge_model_service_url = ''  # Set this to where your judge LLM is hosted
gconfig.OPENAI_API_KEY = getenv('OPENAI_API_KEY')
gconfig.OPENAI_BASE_URL = getenv('OPENAI_BASE_URL')  # Set this to where your other LLMs will be hosted, None for default

# Create an MCP server on port 3000 with two test servers
# Ensure both test servers are running by starting them in the terminal before starting demo_master_server.py
server = MasterMCPServer(
    port=3000,
    sub_servers=[
        # (server url, server identifier) pairs - ensure all server identifiers are unique
        # If the server is located locally (as the demo servers are), ensure the server identifier matches the server's file name (without the .py)
        ("http://localhost:8091/mcp", 'test_server_1'),
        ("http://localhost:8092/mcp", 'test_server_2')
    ]
)
server.startup()

您可以通过运行以下命令在终端中运行此服务器:

python master_server.py

主MCP客户端

MCP客户端 利用可流式传输的HTTP协议可以无需任何修改即可连接到主MCP服务器。URL中提供了详细信息。

有关MCP客户端的完整工作示例,请参阅 MCP GitHub仓库.

许可证

此项目根据MIT许可证获得许可-有关详细信息,请参阅许可证文件。