请点赞,朋友们!
在 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
在 src/handles 目录下添加自己的工具类,参考示例 example.py 样本。
BaseHandlername 属性,这是工具的名字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",
]
当前,此框架支持所有模型上下文协议(MCP)传输模式(STDIO、SSE、可流式HTTP)。
在你的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
在你的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
在你的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"
}
}
}
}