返回市场
简易MCP

简易MCP

作者:wenb1n-dev2 星标更新:2025-06-06

项目介绍

简体中文 English

easyMcp

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

请点赞,朋友们!

用户手册

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

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

# MySQL 数据库配置
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 = (
        "这是一个示例工具"
    )

    def get_tool_description(self) -> Tool:
        return Tool(
            name=self.name,
            description=self.description,
            inputSchema={
                "type": "object",
                "properties": {
                    "text": {
                        "type": "string",
                        "description": "这是必须输入的示例文本"
                    }
                },
                "required": ["text"]
            }
        )

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

            try:
                if "text" not in arguments:
                    raise ValueError("缺少文本内容")

                text = arguments["text"]

                # 引用配置信息
                config = get_config()

                ## todo something

                result = "xxxxxxx"

                # 将所有结果用逗号连接
                return [TextContent(type="text", text=','.join(result))]

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

init.py

from .example import Example


__all__ = [
    "Example",
]

3. 启动

当前,此框架支持所有模型上下文协议(MCP)传输模式(STDIO、SSE、可流式HTTP)。

1. 可流式HTTP模式

  • 使用uv启动服务

在你的MCP客户端工具中添加以下内容,如cursor、cline等。

mcp json 示例:

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

启动命令

# 安装依赖
uv sync

# 启动
uv run src/server.py

2. SSE模式

  • 使用uv启动服务

在你的MCP客户端工具中添加以下内容,如cursor、cline等。

mcp json 示例:

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

启动命令

# 安装依赖
uv sync

# 启动
uv run src/server.py --sse

3. STDIO模式

在你的MCP客户端工具中添加以下内容,如cursor、cline等。

mcp json 示例:

{
  "mcpServers": {
      "operateMysql": {
        "isActive": true,
        "name": "operateMysql",
        "command": "uv",
        "args": [
          "--directory",
          "G:\\python\\mysql_mcp\\src",  # 替换为你自己的项目路径
          "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"
       }
    }
  }
}    

截图

image