MCP项目
MCP服务器是轻量级程序,通过标准化协议为AI应用程序(如Claude、ChatGPT或Cursor等AI增强IDE)提供对外部工具、数据源或服务的访问。它们充当人工智能模型和外部系统之间的桥梁,实现无缝交互,而不需要为每个工具或数据源进行自定义集成。将MCP服务器视为“适配器”,将来自AI应用程序的请求转换为特定系统(如GitHub、Slack、数据库或文件系统)的操作或数据检索。
MCP(模型上下文协议)项目是一个模块化系统,通过主MCP服务器集成了多种服务(SQLite、文件系统、AWS IAM、Gmail),可通过为Claude优化的基于Streamlit的自定义web界面访问。为了支持其他AI模型(例如Grok、OpenAI),您需要修改 client_streamlit.py 代码。
备注:该项目是专门为Claude构建的,使用Streamlit中编写的自定义Claude UI,利用Claude API,这比Claude桌面应用程序更具成本效益。如果愿意,它还支持通过Claude桌面应用程序进行交互。这是一个开发工具,需要在运行前进行特定配置(例如,API密钥、SMTP设置、文件路径)。它不是一键式解决方案,旨在让用户轻松设置和调试开发环境。
项目结构
/
├── client_streamlit.py # Streamlit client for user interaction with Claude UI
├── mcp_server_master.py # Master MCP server mounting subservers
├── config/
│ ├── config_loader.py # Shared module for loading configuration
│ └── mcp_client_config.json # Configuration file for all components
├── servers/
│ ├── sqlite/
│ │ └── mcp_server_sqlite.py # SQLite subserver for database operations
│ ├── filesystem/
│ │ └── filesystem_mcp.py # Filesystem subserver for file operations
│ ├── aws/
│ │ └── aws_iam_mcp.py # AWS IAM subserver for AWS operations
│ ├── gmail/
│ │ ├── gmail_mcp.py # Gmail subserver for email operations (SMTP)
│ │ └── smtp_config.json # SMTP configuration for Gmail subserver
├── test/ # Allowed directory for Filesystem operations
├── conversation.db # SQLite database for conversation history
├── candidates.db # SQLite database for candidate data
├── requirements.txt # Python dependencies
├── mcp-server-master.log # Master server log
├── filesystem-mcp.log # Filesystem subserver log
├── servers/aws/aws-iam-mcp.log # AWS IAM subserver log
├── servers/gmail/gmail-mcp.log # Gmail subserver log
└── servers/sqlite/sqlite-mcp.log # SQLite subserver log备注:替换 使用您的项目目录路径(例如。, /home/user/mcp 或 C:\mcp 在Windows上)。
特性
- Streamlit客户端:为Claude构建的自定义web界面,通过代码修改支持其他AI模型(Grok、OpenAI)
client_streamlit.py. - 主MCP服务器:中央服务器安装子服务器,能够安装额外的自定义或可用的MCP服务器以实现扩展功能。
- 子服务器:
- 数据库:管理 candidates.db 使用SQL查询工具(read_query, write_query, list_tables, describe_table)、候选资产更新和薪资查询。 - 文件系统:处理中的文件操作 /test (例如,读取、写入、搜索、目录列表)。 - AWS IAM:管理AWS IAM用户、组、角色、策略、访问密钥和成本数据。 - Gmail:使用设置通过SMTP发送电子邮件 smtp_config.json.
- 人工智能集成:通过Claude API为Claude优化,通过自然语言查询和工具执行的代码更改支持Grok和OpenAI模型。
- 配置:集中式JSON配置(
mcp_client_config.json)对于所有组件,SMTP设置在smtp_config.json对于Gmail。
先决条件
- 操作系统:Linux(在Ubuntu或WSL2上测试)
- python:3.8或更高
- ngrok:用于暴露主服务器(外部访问可选)
- AWS凭据:已配置
~/.aws/credentials用于 AWS IAM 操作 - SMTP凭据:已配置 `
/servers/gmail/smtp_config.json` 用于Gmail电子邮件发送
- 无烟煤API密钥:用于克劳德模型集成
安装说明(Linux)
1.克隆或设置项目目录
mkdir -p
cd
# Copy all project files (client_streamlit.py, mcp_server_master.py, etc.) into
2.安装依赖项
创建并激活虚拟环境,然后从安装依赖项 requirements.txt:
python -m venv venv
source venv/bin/activate
pip install -r requirements.txt备注:The fastmcp 包装可以是定制的。如果不在PyPI上,请手动安装:
pip install /path/to/fastmcp.whl
# or
pip install git+3.配置项目
创建主配置文件:
mkdir -p
/config
nano
/config/mcp_client_config.json粘贴以下内容,替换 YOUR_ANTHROPIC_API_KEY 使用您的Anthropic API密钥和 `` 使用您的ngrok网址:
{
"anthropicApiKey": "YOUR_ANTHROPIC_API_KEY",
"dbPath": "
/conversation.db",
"mcpServers": {
"master-server": {
"url": "https://.ngrok-free.app/mcp",
"serverName": "MasterMCPServer",
"port": 8006
},
"filesystem-server": {
"allowedDir": "
/test",
"serverName": "FilesystemServer",
"port": 8002
},
"aws-server": {
"serverName": "AWSServer"
},
"gmail-server": {
"serverName": "GmailServer"
},
"sqlite-server": {
"serverName": "SqliteServer",
"port": 8000,
"dbPath": "
/candidates.db"
}
}
}设置权限:
chmod 600
/config/mcp_client_config.json4.为Gmail配置SMTP
创建SMTP配置文件:
mkdir -p
/servers/gmail
nano
/servers/gmail/smtp_config.json粘贴以下内容,用您的Gmail SMTP凭据替换占位符:
{
"smtp_server": "smtp.gmail.com",
"smtp_port": 587,
"smtp_username": "your.email@gmail.com",
"smtp_password": "your-app-specific-password"
}备注:使用 应用程序密码 为了 smtp_password 如果您的Gmail帐户启用了两步验证。
设置权限:
chmod 600
/servers/gmail/smtp_config.json5.设置ngrok(可选)
对于主服务器的外部访问:
ngrok http 8006更新 mcpServers.master-server.url 在……里面 mcp_client_config.json 使用ngrok URL(例如。, https://.ngrok-free.app/mcp).
6.配置AWS凭据
为IAM和Cost Explorer操作设置AWS凭据:
mkdir -p ~/.aws
nano ~/.aws/credentials添加:
[default]
aws_access_key_id = YOUR_AWS_ACCESS_KEY
aws_secret_access_key = YOUR_AWS_SECRET_KEY设置权限:
chmod 600 ~/.aws/credentials7.创建目录并设置权限
mkdir -p
/test
mkdir -p
/servers/{sqlite,filesystem,aws,gmail}
chmod 755
/test
chmod 755
/servers/{sqlite,filesystem,aws,gmail}
chmod 644
/*.py
chmod 644
/config/config_loader.py
chmod 644
/servers/*/*.py8.初始化数据库
Streamlit客户端和SQLite子服务器将创建 conversation.db 和 candidates.db 如果它们不存在,则自动执行。验证:
ls -l
/{conversation.db,candidates.db}
chmod 644
/{conversation.db,candidates.db}运行应用程序
- 启动主服务器:
cd
source venv/bin/activate
pkill -f mcp_server_master.py
python mcp_server_master.py &
tail -f
/mcp-server-master.log验证日志是否显示已安装的子服务器(例如。, Mounted sqlite-server, Mounted gmail-server).
- 启动Streamlit客户端:
cd
source venv/bin/activate
streamlit run client_streamlit.py访问UI http://localhost:8501.
- 可选:独立运行子服务器 (用于调试):
- 文件系统:
cd
/servers/filesystem
pkill -f filesystem_mcp.py
python filesystem_mcp.py &
tail -f
/filesystem-mcp.log- SQLite:
cd
/servers/sqlite
pkill -f mcp_server_sqlite.py
python mcp_server_sqlite.py &
tail -f
/servers/sqlite/sqlite-mcp.log用法
- 配置页面:
- 导航到Streamlit UI中的配置页面。 - 输入您的Anthropic API密钥并将其保存以进行更新 mcp_client_config.json.
- 聊天页面:
- 通过聊天输入进行交互,例如: - 数据库:“从候选人限制3中选择\*”,“更新候选人资产John Doe Tablet” - 文件系统:“搜索\*.txt文件”,“创建web应用程序”(in /test) - Gmail:“发送电子邮件”(提示收件人、主题、消息) - AWS IAM:“列出IAM用户”,“获取成本数据” - 在侧边栏中查看可用工具(例如。, sqlite-server.read.query, gmail-server.emails.send.email).
- 切换AI模型:
- 默认值:Claude(通过Claude API优化) client_streamlit.py). - 对于Grok或OpenAI,请更新 client_streamlit.py:
from openai import OpenAI
st.session_state.client = OpenAI(api_key="YOUR_OPENAI_API_KEY")添加 openai==1.51.0 向 requirements.txt 并重新安装:
pip install -r requirements.txt- 对于Grok,请使用xAI API(请参见https://x.ai/api).
- 安装其他MCP服务器:
- 要安装自定义或其他MCP服务器,请更新 mcp_server_master.py 以包括新服务器:
from servers.custom.custom_mcp import mcp as custom_mcp
master_mcp.mount(custom_mcp, prefix="custom-server")- 将服务器添加到 mcp_client_config.json:
"custom-server": {
"serverName": "CustomServer",
"port": 8003
}已知Bug
- 路径配置:您可能需要手动更新中的文件路径
mcp_client_config.json或其他脚本,如果 `
与您的项目目录结构不匹配。双重检查路径 dbPath, allowedDir,以及 smtp_config.json`.
故障排除
- 日志:
tail -f
/mcp-server-master.log
tail -f
/servers/sqlite/sqlite-mcp.log
tail -f
/filesystem-mcp.log
tail -f
/servers/aws/aws-iam-mcp.log
tail -f
/servers/gmail/gmail-mcp.log- 配置问题:
cat
/config/mcp_client_config.json
chmod 600
/config/mcp_client_config.json
cat
/servers/gmail/smtp_config.json
chmod 600
/servers/gmail/smtp_config.json- 文件系统访问:
ls -ld
/test
mkdir -p
/test
chmod 755
/test- 数据库访问:
ls -l
/{conversation.db,candidates.db}
chmod 644
/{conversation.db,candidates.db}- SMTP问题:
- 确保 smtp_config.json 具有有效凭据。 - 验证Gmail 不太安全的应用程序 或应用程序密码设置。 - 测试SMTP连接:
python -c "import smtplib; s = smtplib.SMTP('smtp.gmail.com', 587); s.starttls(); s.login('your.email@gmail.com', 'your-app-specific-password')"- AWS凭据:
cat ~/.aws/credentials
chmod 600 ~/.aws/credentials- ngrok URL已过期:
更新 mcpServers.master-server.url 在……里面 mcp_client_config.json:
nano
/config/mcp_client_config.json依赖项
看 requirements.txt 完整列表:
fastmcp
streamlit==1.38.0
anthropic==0.34.2
anyio==4.6.0
pydantic==2.9.2
boto3==1.35.24
google-auth==2.35.0
google-auth-oauthlib==1.2.1
google-auth-httplib2==0.2.0
google-api-python-client==2.149.0备注:替换 fastmcp 如果不在PyPI上,则使用其安装源。Gmail子服务器使用SMTP,因此如果仅使用SMTP,则可能不需要Google API软件包;将它们从 requirements.txt 如果未使用。
贡献
将问题或拉取请求提交到项目存储库(如果适用)。确保代码更改与现有的FastMCP框架和配置结构保持一致。
