书籍搜索与聊天客户端

一个完整的模型上下文协议(MCP)实现,包含自定义书籍搜索服务器和基于Streamlit的聊天客户端,用于与本地LLMs集成并通过Google Books API搜索书籍。

  • by jahleeljackson
  • Python
  • 本地部署
  • 更新于
在 GitHub 查看 jahleeljackson/mcp-project

使用说明

MCP项目

一个完整的模型上下文协议(MCP)实现,包括一个自定义书籍搜索服务器和一个基于Streamlit的聊天客户端。该项目展示了如何使用Ollama将MCP服务器与Claude Desktop和本地大型语言模型(LLM)进行构建和集成。

🎯 概述

这个项目由两个主要部分组成:

  1. MCP服务器 (mcp-server/这是一个自定义的MCP服务器,它利用谷歌图书API并结合智能缓存功能,提供书籍搜索能力
  2. MCP 客户端 (mcp-client/): 一个通过Ollama连接到本地大型语言模型(LLM)的Streamlit网页界面,用于与MCP服务器进行交互

🌟 特点

MCP服务器

  • 使用Google图书API进行书籍搜索
  • 💾 智能缓存系统(最多支持100本书)
  • 🔌 MCP工具、资源和提示
  • ⚡ 利用本地缓存快速重复查询

MCP 客户端

  • 💬 使用Streamlit构建的交互式聊天界面
  • 通过Ollama(默认为Mistral)与本地大型语言模型(LLMs)集成
  • 🔗 通过Ollama MCP桥连接到MCP服务器
  • 🎨 简洁、用户友好的界面

📋 先决条件

🚀 快速入门

1. 设置MCP服务器

cd mcp-server

# Install dependencies
uv sync

# Create .env file with your Google Books API key
echo "API_KEY=your_google_books_api_key_here" > .env

# Test the server
uv run server.py

获取Google图书API密钥:

2. 设置MCP客户端

cd mcp-client

# Install dependencies
uv sync

# Ensure Ollama is running
ollama pull mistral:latest  # or your preferred model

# Start the Ollama MCP Bridge
ollama-mcp-bridge

# In a new terminal, run the Streamlit app
streamlit run frontend.py

3. 配置MCP服务器

编辑 mcp-client/mcp-config.json 配置要使用的MCP服务器:

{
  "mcpServers": {
    "find-books": {
      "command": "uv",
      "args": [
        "--directory",
        "/absolute/path/to/mcp-server",
        "run",
        "server.py"
      ]
    }
  }
}

📁 项目结构

mcp_project/
├── mcp-server/              # MCP Server implementation
│   ├── server.py           # Main server code
│   ├── pyproject.toml      # Server dependencies
│   ├── .env               # API keys (not in git)
│   └── books_cache.txt    # Book cache storage
│
├── mcp-client/              # Streamlit chat client
│   ├── frontend.py         # Streamlit UI
│   ├── pyproject.toml      # Client dependencies
│   └── mcp-config.json     # MCP server configuration
│
├── .gitignore
└── README.md               # This file

💡 使用示例

一旦两个组件都运行起来,您就可以通过Streamlit界面与MCP服务器进行交互:

搜索书籍:

Can you search for "The Great Gatsby"?

找到多本书:

Tell me about "1984" and "Brave New World"

获取最新书籍:

What's the most recent book that was searched?

生成主题摘要:

Can you summarize the themes from cached books?

🔧 配置

客户端配置(mcp-client/pyproject.toml)

[project]
model = "mistral:latest"  # Change to your preferred Ollama model
host = "0.0.0.0"
port = "8000"

服务器配置

环境变量在 mcp-server/.env:

API_KEY=your_google_books_api_key

🧪 使用Claude桌面版进行测试

MCP服务器也可以直接与Claude桌面端配合使用:

cd mcp-server
uv run mcp install server.py

或者手动配置在 ~/Library/Application Support/Claude/claude_desktop_config.json (macOS)(苹果电脑操作系统)。

🛠️ 开发

独立运行服务器

cd mcp-server
uv run server.py

独立运行客户端

cd mcp-client
streamlit run frontend.py

缓存管理

  • 清除缓存:删除 mcp-server/books_cache.txt
  • 缓存自动维护最近的100本书
  • 服务器重启后缓存仍然保留

📚 MCP 组件

工具

  • get_book(book: str) - 按书名搜索书籍

资源

  • books://latest - 访问最近搜索过的书籍

提示

  • note_summary_prompt() - 生成缓存书籍主题的摘要

🐛 故障排除

Ollama MCP 桥接未连接:

  • 确保Ollama正在运行: ollama list
  • 检查桥接服务是否在8000端口上运行
  • 验证 mcp-config.json 路径是绝对的

服务器未找到书籍:

  • 验证API密钥 .env 文件
  • 检查Google图书API配额/速率限制
  • 使用直接API调用测试API密钥

Streamlit 应用无法加载:

  • 检查端口8501是否可用
  • 验证依赖项是否已安装: uv sync
  • 检查终端中的错误信息

📖 了解更多

📄 许可证

这个项目是出于教育目的。

🤝 贡献(或“参与贡献”)

欢迎随时提交问题和增强请求!


注这是一个用于学习MCP概念和集成模式的小型项目。