Model Context Provider (MCP) Server

hybrid server

The server is able to function both locally and remotely, depending on the configuration or use case.

Integrations

  • Provides GitHub server integration as one of the main server connections available in the MCP

  • Provides integration with Google's AI models (Gemini) for enhanced context understanding and tool usage

  • Provides integration with Ollama for local AI model usage and processing

Model Context Provider (MCP)

Model Context Provider 是一個創新的 AI 開發工具,專門設計用於增強大型語言模型(LLMs)的上下文理解和工具使用能力。它提供了一個統一的介面,讓開發者能夠更有效地管理和使用各種 AI 模型及其相關工具。

什麼是 MCP?

MCP 的核心理念是「上下文增強」和「工具協調」。它通過以下方式來實現這一目標:

  1. 上下文管理
    • 智能處理和組織輸入資訊
    • 維護對話歷史和狀態
    • 動態調整系統提示(System Prompts)
    • 優化上下文窗口的使用
  2. 工具整合
    • 統一的工具調用介面
    • 自動工具選擇和參數配置
    • 支援多種工具類型:
      • 代碼分析工具
      • 文件操作工具
      • 資源檢索工具
      • 外部 API 調用
  3. 模型協調
    • 支援多種 AI 提供者:
      • OpenAI (GPT-4, GPT-3.5)
      • Anthropic (Claude)
      • Google (Gemini)
      • Ollama (本地模型)
    • 智能模型選擇
    • 負載平衡和故障轉移

主要特點

1. 智能上下文處理

  • 自動管理上下文長度
  • 智能摘要和重要信息保留
  • 動態調整提示策略

2. 強大的工具生態

  • 內建多種開發工具
  • 可擴展的工具介面
  • 自動工具鏈組合

3. 開發者友好

  • RESTful API
  • 圖形化介面
  • 完整的開發文檔
  • 豐富的示例

4. 企業級特性

  • 高可用性設計
  • 安全性控制
  • 使用量監控
  • 日誌追蹤

應用場景

1. 軟體開發

  • 代碼審查和重構
  • API 文檔生成
  • 測試用例生成
  • 除錯協助

2. 知識管理

  • 文檔分析和總結
  • 知識庫建立
  • 資訊檢索和組織

3. 工作流自動化

  • 自動化腳本生成
  • 工作流程優化
  • 任務協調

4. 研究與分析

  • 數據分析
  • 文獻研究
  • 報告生成

安裝需求

安裝 UV

pip install uv

安裝依賴

uv pip install fastapi uvicorn gradio google-generativeai ollama

重新同步依賴項

uv sync

設定 API 金鑰

OpenAI

SET OPENAI_API_KEY='your-api-key'

Anthropic (選用)

SET ANTHROPIC_API_KEY='your-api-key'

Google (選用)

SET GOOGLE_API_KEY='your-api-key'

Ollama (選用)

SET OLLAMA_HOST='http://localhost:11434'

使用方式

1. FastAPI Web 介面

啟動 Web API 伺服器:

python -m mcpcli.web --openai-key sk-xxx... --anthropic-key sk-ant-xxx... --google-key xxx...

命令列參數

  • --openai-key 指定 OpenAI API 金鑰
  • --anthropic-key 指定 Anthropic API 金鑰
  • --google-key 指定 Google API 金鑰

伺服器將在 http://localhost:7860 啟動,你可以使用 Swagger UI(http://localhost:7860/docs)來測試 API。

主要 API 端點:

  • /connect/{server_name} - 連接到指定伺服器
  • /chat - 發送聊天訊息
  • /tools - 列出可用工具
  • /resources - 列出可用資源
  • /prompts - 列出可用提示

2. Gradio 圖形介面

啟動 Gradio 介面:

python -m mcpcli.web.gradio_app --openai-key sk-xxx... --anthropic-key sk-ant-xxx... --google-key xxx... --port 8082

命令列參數

  • --openai-key 指定 OpenAI API 金鑰
  • --anthropic-key 指定 Anthropic API 金鑰
  • --google-key 指定 Google API 金鑰
  • --port 指定 Gradio 端口,預設為 8082

介面將在 http://localhost:8082 啟動,你可以使用圖形化介面來與模型互動。

使用步驟:

  1. 連接伺服器:
    • 輸入伺服器名稱(預設為 "github")
    • 可選擇指定端口
    • 點擊「連接伺服器」按鈕
  2. 配置 AI 設定:
    • 選擇 AI 提供者
    • 輸入模型名稱
  3. 開始對話:
    • 在輸入框中輸入訊息
    • 點擊「發送」按鈕
    • 使用「清除歷史」按鈕重置對話

3. cli介面

使用python執行

python -m mcpcli.cli --server github --provider ollama --model llama3.2 --openai-key sk-xxx... --anthropic-key sk-ant-xxx... --google-key xxx...

使用uv執行

uv run mcp-cli --server github --provider ollama --model llama3.2

命令列參數

  • --server 指定伺服器名稱,預設為github
  • --host 指定伺服器主機,預設為127.0.0.1
  • --port 指定伺服器端口,預設為8765
  • --openai-key 指定 OpenAI API 金鑰
  • --anthropic-key 指定 Anthropic API 金鑰
  • --google-key 指定 Google API 金鑰
  • --provider 指定 AI 提供者(openai, anthropic, google, ollama),預設為 openai
  • --model 指定模型名稱
  • --config-file 指定配置文件路徑,預設為server_config.json

常見問題解決

  1. 連接錯誤:
    • 確認伺服器名稱是否正確
    • 檢查端口是否被占用
    • 確認環境變數是否正確設定
  2. API 金鑰相關:
    • 確保已設定正確的 API 金鑰
    • 檢查 API 金鑰是否過期
    • 確認 API 使用額度
  3. 工具調用失敗:
    • 檢查工具名稱是否正確
    • 確認參數格式是否符合要求
    • 查看日誌以獲取詳細錯誤信息

開發者指南

添加新的 AI 提供者

  1. llm_client.py 中添加新的提供者支援
  2. 更新 Gradio 介面中的提供者選項
  3. 確保正確處理新提供者的 API 響應

自定義工具

  1. 在工具配置文件中添加新工具定義
  2. 實現工具的具體功能
  3. 更新工具文檔

注意事項

  • API 金鑰安全:請勿在公共環境中暴露你的 API 金鑰
  • 資源使用:注意監控 API 使用量,避免超出限額
  • 錯誤處理:建議實作錯誤重試機制
  • 日誌記錄:保持適當的日誌級別以便排查問題

貢獻指南

歡迎提交 Pull Request 來改進這個專案!請確保:

  1. 代碼符合 PEP 8 規範
  2. 添加適當的測試用例
  3. 更新相關文檔
  4. 提供清晰的提交信息

授權

本專案採用 MIT 授權條款 - 詳見 LICENSE 文件