Llama与GitHub集成的模型上下文协议服务器
此存储库包含一个模型上下文协议(MCP)服务器实现,该实现与本地运行的Llama模型集成,并提供GitHub存储库文件列表功能。MCP服务器利用来自本地LLM和GitHub存储库的相关信息增强AI应用程序。
概述
该项目由两个主要部分组成:
- MCP服务器 -基于FastAPI的服务器:
- 实现模型上下文协议 - 将查询转发到本地呼叫模型 - 列出GitHub存储库中的文件
- Python客户端 -演示如何与MCP服务器交互的示例客户端应用程序
先决条件
- Python 3.7或更高版本
- 运行调用模型服务器(例如,Ollama)athttp://localhost:11434/
- 用于访问GitHub API的Internet连接
- Git已安装在您的计算机上
- GitHub账号
安装
克隆存储库
git clone https://github.com/YourUsername/mcp-llama-integration.git
cd mcp-llama-integration再进行
pip install -r requirements.txt文件结构
mcp-llama-integration/
├── llama_mcp_server.py # MCP server with Llama and GitHub integration
├── llama_client_app.py # Sample client application
└── README.md # Project documentation设置火焰模型
- 如果还没有,请安装 没有
- 拉动火焰模型:
ollama pull llama3.2- 验证模型是否正在运行:
curl http://localhost:11434/api/tags
http://localhost:11434
http://localhost:11434/api/tags运行MCP服务器
- 启动服务器:
python llama_mcp_server.py- 服务器将在上开始运行
http://localhost:8000
- 您可以通过检查运行状况终结点来验证服务器是否正在运行:
curl http://localhost:8000/health使用客户端应用程序
- 在单独的终端中,启动客户端应用程序:
python llama_client_app.py- 应用程序将提示您输入
- 键入您的查询并接收Llama模型的回复
- 要列出GitHub存储库中的文件,请使用以下查询:
List files from https://github.com/username/repository
List files from https://github.com/EXPESRaza/mcp-llama-github-integration- 键入“exit”退出应用程序
API文档
MCP服务器端点
POST/上下文
请求给定查询的上下文。还可以处理GitHub存储库列表。
请求正文:
{
"query_text": "Your query here or 'List files from https://github.com/username/repo'",
"user_id": "optional-user-id",
"session_id": "optional-session-id",
"additional_context": {}
}答复:
{
"context_elements": [
{
"content": "Response from Llama model or GitHub file listing",
"source": "llama_model or github_api",
"relevance_score": 0.9
}
],
"metadata": {
"processing_time_ms": 150,
"query": "Your query here"
}
}POST/github/列表文件
用于在GitHub存储库中列出文件的专用端点。
请求正文:
{
"repo_url": "https://github.com/username/repository"
}答复:
{
"files": ["📄 README.md", "📁 src", "📄 LICENSE"],
"repository": "repository",
"owner": "username",
"success": true,
"message": null
}GET/健康
检查MCP服务器及其连接的运行状况。
答复:
{
"status": "healthy",
"llama_status": "connected",
"github_status": "connected"
}定制
改变火焰模型
如果你想使用不同的Llama模型,请修改 model 参数在 query_llama 功能在 llama_mcp_server.py:
payload = {
"model": "your-model-name", # Change this to your model name
"prompt": text,
"stream": False
}修改GitHub API集成
服务器当前仅列出存储库根目录中的文件。要支持子目录或其他GitHub功能,请修改 list_github_repo_files 功能在 llama_mcp_server.py.
错误处理
该应用程序包括针对Llama模型和GitHub API的强大错误处理:
- 如果Llama型号不可用,将返回错误消息
- 如果GitHub存储库URL无效,将通知用户
- 如果GitHub API返回错误(例如,未找到存储库、速率限制),将显示相应的错误消息
故障排除
常见问题
- 连接被拒绝错误
- 确保火焰模型在运行http://localhost:11434/ - 验证Ollama是否正确安装并运行
- GitHub API 错误
- 检查存储库URL是否正确 - GitHub API对未经验证的请求有速率限制
- 未找到GitHub存储库
- 验证存储库是否存在并且是公共的 - 私有存储库需要身份验证(此版本中未实现)
贡献
欢迎投稿!请随时提交拉取请求。
许可证
此项目根据MIT许可证获得许可-有关详细信息,请参阅许可证文件。
