AI代理工具MCP服务器

一个快速轻量级的MCP服务器,为AI代理提供多种工具支持,包括STDIO模式和SSE模式。

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

使用说明

带有AI代理工具的简单MCP服务器

快速轻便 MCP服务器 使用不同的AI代理工具。它支持STDIO(克劳德桌面)和SSE(远程代理)。

索引


先决条件

  • Python>=3.11
  • 已安装紫外线和管道

↑ index


配置

初始化 虚拟环境 并通过以下方式安装依赖项:

uv venv
source .venv/bin/activate
uv sync

创造你的 .env 通过复制文件:

cp env.dist .env

然后,根据需要进行自定义。

↑ index


在STDIO模式下运行服务器

首先,要测试服务器,请安装并运行MCP检查器:

npx @modelcontextprotocol/inspector uv run python -m app.main

最后,用户界面将在浏览器中打开。通过单击连接到服务器 连接 在左边的菜单上。

然后,从顶部栏中单击 工具 和 列出工具。此时,您可以选择您喜欢的工具并使用它。

如果你想在没有检查器的情况下进行测试,只需使用以下命令启动:

uv run python -m app.main

↑ index


在SSE模式下运行服务器

如果你想通过使用服务器 上海证券交易所 从远程代理启动它:

uv run python -m app.main --sse --port 8000

作为STDIO模式,您可以使用MCP Inspector(远程)进行测试:

npx @modelcontextprotocol/inspector

如果要模拟来自远程代理的工具调用,请创建一个简单的 STDIO客户端 在python中(例如。 stdio_test.py)使用此代码:

import asyncio
from mcp import ClientSession, StdioServerParameters
from mcp.client.stdio import stdio_client

async def test():
    server_params = StdioServerParameters(command="uv", args=["run", "python", "server.py"], cwd="./")
    
    async with stdio_client(server_params) as (read, write):
        async with ClientSession(read, write) as session:
            await session.initialize()
            
            # Tools list
            tools = await session.list_tools()
            print("Tools:", [t.name for t in tools.tools])
            
            # Call calculate
            result = await session.call_tool("calculate", { "operation": "multiply", "a": 6, "b": 7 })
            print("Result:", result.content)

asyncio.run(test())

然后运行:

python3 stdio_test.py

你会看到一个 可用工具列表 以及结果 calculate.

↑ index


配置Claude桌面

如果你想在 克劳德桌面版,创建文件 claude_desktop_config.json 包含以下内容:

{
  "mcpServers": {
    "mcp-server-tools": {
      "command": "uv",
      "args": ["run", "--directory", "/path/to/mcp-server", "python", "-m", "app.main"]
    }
  }
}

将此文件移入:

  • macOS: ~/Library/Application Support/Claude
  • 窗户: %APPDATA%\Claude

↑ index


可用工具

工具 描述
calculate 数学运算(加、减、乘、除、幂)
get_datetime 带时区和可配置格式的日期/小时
process_text 文本处理程序(字数统计、提取电子邮件/URL、统计数据)
fetch_url HTTP GET/HEAD请求
convert_data JSON、Base64、十六进制转换

↑ index


创建新工具

要创建新工具,您需要:

  • 创建新文件(例如。 app/tools/my_new_tool.py)

  • 写下你的逻辑,保持这个结构:

    from app.mcp import mcp
    
    @mcp.tool()
    def my_new_tool(your_param: str) -> dict[str, Any]:
      """
      Clear and exaustive tool description.
    
      Args:
          your_param: clear and exaustive param description
    
      Returns:
          Clear and exaustive result description
      """
    
      # YOUR LOGIC HERE
    
      if some_error:
          return {"success": False, "error": "Clear error description"}
      
      return {
          "success": True,
          "your_resp": "...",
          "other_resp": "...",
      }
    
  • 编辑 app/tools/__init__.py 文件并添加您的工具:

    from app.tools import my_new_tool
    
    __all__ = [
        "my_new_tool",
    ]
    
  • 重新启动服务器

同样地,如果你想 删除 一个现有的工具,只需将其从中删除即可 __init__.py 并删除相关 .py 文件。

↑ index


VSCode中的调试

要调试Python微服务,您需要:

  • 安装 VSCode
  • 确保你有 Python 扩展 安装
  • 确保您已选择 使用virtualenv的右解释器 在VSCode上
  • 点击 运行和调试 菜单和 创建launch.json文件
  • 从下拉列表中选择 Python调试器 和 快速API
  • 更改 .vscode/launch.json 在项目根目录中创建(如果更改,则自定义主机和端口):
{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "MCP Server (SSE)",
      "type": "debugpy",
      "request": "launch",
      "module": "app.main",
      "args": [
          "--sse",
          "--port", "8000",
          "--reload"
      ],
      "envFile": "${workspaceFolder}/.env",
      "console": "integratedTerminal",
      "cwd": "${workspaceFolder}",
      "justMyCode": true
    },
    {
      "name": "MCP Server (STDIO)",
      "type": "debugpy",
      "request": "launch",
      "module": "app.main",
      "args": [
          "--reload"
      ],
      "envFile": "${workspaceFolder}/.env",
      "console": "integratedTerminal",
      "cwd": "${workspaceFolder}",
      "justMyCode": true
    }
  ]
}
  • 在代码中添加一些断点,然后按 绿色播放按钮
  • 调用API进行调试

↑ index


由...制作♥️ 作者:亚历山德罗·奥鲁