MCP服务器框架

一个快速构建支持所有MCP传输模式(STDIO、SSE、Streamable Http)的可扩展服务器框架。

  • by wenb1n-dev
  • Python
  • 本地部署
  • 2 星标
  • 更新于
在 GitHub 查看 wenb1n-dev/easymcp

使用说明

easyMcp

只需两步即可快速构建一个易于扩展的mcp服务器框架,该框架支持所有模型上下文协议(mcp)传输模式(STDIO、SSE、流式Http)。

朋友们,请点赞!

用户手册

1.第一步[可选]定义所需的配置信息

在中定义项目所需的配置信息 src/config/.env,例如,如果您需要数据库配置:

 # MySQL Database Configuration
MYSQL_HOST=192.168.3.229
MYSQL_PORT=3306
MYSQL_USER=root
MYSQL_PASSWORD=root
MYSQL_DATABASE=a_llm
MYSQL_ROLE=admin

2.第二步创建自己的工具

在中添加您自己的工具类 src/handles 目录,请参考example.py示例

  • 从BaseHandler继承
  • 定义 name 属性,即工具的名称
  • 定义 description 属性,即工具的描述
  • 实施 get_tool_description 方法,该方法告诉mcp客户端您的工具的功能
  • 实施 run_tool 方法,即mcp客户端调用该工具的方法;在此处实现您的工具逻辑
  • 将新工具导入 __init__.py

example.py

from typing import Dict, Any, Sequence

from mcp import Tool
from mcp.types import TextContent
from .base import BaseHandler
from config import get_config


class Example(BaseHandler):
    name = "get_Example"
    description = (
        "this is Example xxxx"
    )

    def get_tool_description(self) -> Tool:
        return Tool(
            name=self.name,
            description=self.description,
            inputSchema={
                "type": "object",
                "properties": {
                    "text": {
                        "type": "string",
                        "description": "This is the text that must be entered for the example"
                    }
                },
                "required": ["text"]
            }
        )

    async def run_tool(self, arguments: Dict[str, Any]) -> Sequence[TextContent]:

            try:
                if "text" not in arguments:
                    raise ValueError("Missing text content")

                text = arguments["text"]

                # Reference configuration information
                config = get_config()

                ## todo something

                result = "xxxxxxx"

                # Join all results with commas
                return [TextContent(type="text", text=','.join(result))]

            except Exception as e:
                return [TextContent(type="text", text=f"error: {str(e)}")]

__初始化__南美国家巴拉圭的缩写(Paraguay)

from .example import Example


__all__ = [
    "Example",
]

3.开始

目前,该框架支持所有模型上下文协议(MCP)传输模式(STDIO、SSE、流式Http)。

1.流式Http模式

  • 使用uv启动服务

将以下内容添加到您的mcp客户端工具中,如游标、cline等。

mcp-json示例:

{
  "mcpServers": {
    "easyMcp": {
      "name": "easyMcp",
      "type": "streamableHttp",
      "description": "",
      "isActive": true,
      "baseUrl": "http://localhost:3000/mcp/"
    }
  }
}

启动命令

# Install dependencies
uv sync

# Start
uv run src/server.py

2.SSE模式

  • 使用uv启动服务

将以下内容添加到您的mcp客户端工具中,如游标、cline等。

mcp-json示例:

{
  "mcpServers": {
    "easyMcp": {
      "name": "easyMcp",
      "description": "",
      "isActive": true,
      "baseUrl": "http://localhost:9000/sse"
    }
  }
}

启动命令

# Install dependencies
uv sync

# Start
uv run src/server.py --sse

3.STDIO模式

将以下内容添加到您的mcp客户端工具中,如游标、cline等。

mcp-json示例:

{
  "mcpServers": {
      "operateMysql": {
        "isActive": true,
        "name": "operateMysql",
        "command": "uv",
        "args": [
          "--directory",
          "G:\\python\\mysql_mcp\\src",  # Replace this with your project path
          "run",
          "server.py",
          "--stdio"
        ],
        "env": {
          "MYSQL_HOST": "192.168.xxx.xxx",
          "MYSQL_PORT": "3306",
          "MYSQL_USER": "root",
          "MYSQL_PASSWORD": "root",
          "MYSQL_DATABASE": "a_llm",
          "MYSQL_ROLE": "admin"
       }
    }
  }
}    

截图