模型控制协议服务器

MCP服务器扩展了Claude Desktop的功能,使其能够与外部系统和API(如AWS服务和GitHub)进行交互。

  • by mounirelbakkali1
  • Python
  • 本地部署
  • 更新于
在 GitHub 查看 mounirelbakkali1/mcp-server-101

使用说明

MCP服务器设置文档

概述

本指南提供了设置与Claude Desktop集成的模型控制协议(MCP)服务器的详细说明。MCP服务器扩展了Claude的功能,允许它与外部系统和API(包括AWS服务和GitHub)进行交互。

先决条件

  • 已安装Python 3.8+
  • Claude桌面应用程序
  • uv 已安装包管理器
  • 基本了解Python和命令行操作

安装步骤

1.安装所需的软件包

首先,确保您拥有必要的软件包:

# Install UV if not already installed
pip install uv

# Install MCP and dependencies using UV
uv add mcp[cli] pygithub boto3

2.创建服务器脚本

创建一个名为的文件 server.py 在您想要的目录中使用自定义函数实现。

示例结构 server.py:

from mcp.server.fastmcp import FastMCP
import boto3
from dotenv import load_dotenv
from github import Github

load_dotenv()

# Initialize MCP server
mcp = FastMCP("Custom MCP Server", "0.1.0")

# Define your custom functions
@mcp.tool()
def fetch_aws_ec2_instances(region_name="us-east-1"):
    """
    Fetches and describes all EC2 instances in the specified AWS region.
    :param region_name: The name of the AWS region (e.g., 'us-east-1')
    :return: A list of EC2 instances in the specified region.
    """
    ec2 = boto3.client('ec2', region_name=region_name)
    return ec2.describe_instances()

# Add more custom tools as needed...

3.测试您的服务器

在连接到Claude Desktop之前,请测试您的服务器以确保其正常工作:

uv run mcp install server.py

您应该看到指示服务器正在运行的输出,通常在端口8000上。

4.配置克劳德桌面

  1. 打开克劳德桌面应用程序
  2. 导航到“文件”>“设置”>“开发人员”>“编辑配置”
  3. 使用您的MCP服务器详细信息更新配置:
{
  "mcpServers": {
    "Custom MCP Server": {
      "command": "uv",
      "args": [
        "run",
        "--with",
        "mcp[cli], pygithub, boto3",
        "mcp",
        "run",
        "/full/path/to/your/server.py"
      ]
    }
  }
}

重要配置说明:

  • 替换 /full/path/to/your/server.py 带有server.py文件的绝对路径
  • 将任何其他必需的包添加到 --with 论点
  • 您可以将“自定义MCP服务器”重命名为您喜欢的任何名称

5.启用并启动MCP服务器

  1. 在Claude Desktop中,转到“文件”>“设置”>“开发人员”
  2. 打开您配置的MCP服务器
  3. Claude将尝试连接到您的服务器

高级配置

环境变量

要安全地处理API密钥和凭据,请使用.env文件:

在与server.py相同的目录中创建.env文件:

.env文件

AWS_ACCESS_KEY_ID=your_ACCESS_KEY_here AWS_SECRET_ACCESS_KEY=your_SECRET_KEY_here GITHUB_TOKEN=你的留言在这里

根据需要添加其他环境变量

添加自定义依赖项

如果你的服务器需要额外的Python包,请将它们添加到 --with Claude Desktop配置中的参数:

"args": [
  "run",
  "--with",
  "mcp[cli], pygithub, boto3, pandas, requests",
  "mcp",
  "run",
  "/path/to/server.py"
]

错误处理

在服务器函数中实现正确的错误处理:

@server.function()
def some_function(param):
    try:
        # Function logic
        return result
    except Exception as e:
        return {"error": str(e), "status": "failed"}

故障排除

常见问题及解决方法

  1. 连接被拒绝

    • 确保服务器正在运行
    • 检查端口是否已在使用中
    • 验证防火墙设置
  2. 导入错误

    • 确认所有依赖项都包含在 --with 论点
    • 尝试手动安装软件包: uv pip install [package-name]
  3. 权限错误

    • 检查server.py的文件权限
    • 确保有适当的AWS/GitHub凭据可用
  4. 未找到函数

    • 验证功能是否正确装饰 @server.function()
    • 重新启动服务器和Claude Desktop

示例用例

AWS资源管理

@server.function()
def describe_ec2_instances(region_name="us-east-1"):
    ec2 = boto3.client('ec2', region_name=region_name)
    return ec2.describe_instances()

@server.function()
def start_ec2_instance(instance_id, region_name="us-east-1"):
    ec2 = boto3.client('ec2', region_name=region_name)
    return ec2.start_instances(InstanceIds=[instance_id])

GitHub集成

@server.function()
def fetch_github_repo_info(repo):
    g = Github(os.getenv("GITHUB_TOKEN"))
    repo = g.get_repo(repo)
    return {
        "name": repo.name,
        "description": repo.description,
        "stars": repo.stargazers_count,
        "forks": repo.forks_count
    }

安全考虑

  • 永远不要在server.py文件中硬编码凭据
  • 对敏感信息使用环境变量
  • 考虑为您的MCP服务器实施身份验证
  • 请注意通过MCP服务器公开哪些功能

资源