关于 MCP Server Setup Documentation
这是一份使用 Python 搭建 MCP 服务并连接 Claude Desktop 的指南,面向希望让 Claude 调用 AWS 和 GitHub API 的开发者。内容涵盖配置、凭据管理、测试及故障排查。
主要功能
- 01Python MCP 服务搭建
- 02配置 Claude Desktop 连接
- 03使用环境变量管理凭据
- 04测试与故障排查
适用场景
- 01向 Claude 提供 AWS 资源管理功能
- 02通过 Claude 查询 GitHub 仓库信息
MCP 相关
指南说明如何创建 MCP 服务,将自定义函数作为工具提供给 Claude Desktop,并介绍本地测试和客户端配置。
配置
本地服务的配置在各客户端中通用,复制后粘贴到客户端的 MCP 配置文件即可。
我们还没有获取到这个服务的安装包或远程地址,请按项目 README 中的说明安装。
工具
本地服务需要在你的电脑上运行后才能读取工具,维护者暂未提供工具列表。
详细介绍
MCP 服务设置文档
概述
本指南详细介绍如何设置与 Claude Desktop 集成的模型控制协议(MCP)服务。MCP 服务可以扩展 Claude 的能力,使其能够与外部系统和 API 交互,包括 AWS 服务和 GitHub。
前提条件
- 已安装 Python 3.8+
- Claude Desktop 应用程序
- 已安装
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. 配置 Claude Desktop
- 打开 Claude Desktop 应用程序
- 前往 File > Settings > Developer > Edit Config
- 使用 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参数中 - 你可以将 "Custom MCP Server" 重命名为任意名称
5. 启用并启动 MCP 服务
- 在 Claude Desktop 中,前往 File > Settings > Developer
- 启用已配置的 MCP 服务
- 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=your_github_token_here
根据需要添加其他环境变量
添加自定义依赖项
如果你的服务需要其他 Python 软件包,请将其添加到 Claude Desktop 配置中的 --with 参数:
"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"}
故障排除
常见问题及解决方案
-
连接被拒绝
- 确保服务正在运行
- 检查端口是否已被占用
- 检查防火墙设置
-
导入错误
- 确认所有依赖项都已包含在
--with参数中 - 尝试手动安装软件包:
uv pip install [package-name]
- 确认所有依赖项都已包含在
-
权限错误
- 检查 server.py 的文件权限
- 确保 AWS/GitHub 凭据可用
-
找不到函数
- 确认函数已使用
@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 服务公开的函数
资源
评论
一个开放协议,规定了 AI 应用如何以统一方式连接外部工具与数据源。客户端负责与模型对话,MCP 服务负责提供工具、资源和提示模板,两者通过标准消息通信,同一个服务可以被不同客户端复用。