模型上下文协议服务器

MCP Server是一个教育性实现,展示AI代理如何通过标准化协议安全地与外部系统交互,提供模块化架构和LLM集成。

  • by pavel-shyrko
  • Python
  • 本地部署
  • 更新于
在 GitHub 查看 pavel-shyrko/mcp-server

使用说明

MCP服务器

MCP(模型上下文协议)服务器 是一个教育实现,演示了人工智能代理如何安全地与外部系统交互。该项目展示了MCP模式——一种人工智能模型通过受控接口访问数据库、API和其他资源的标准化方式。

问题陈述

该项目展示了 概念验证(PoC) 用于使AI模型(LLM)能够通过受控接口访问任何API和系统。挑战在于在人工智能代理和外部资源之间建立一个标准化的桥梁,其架构旨在支持未来的安全和授权机制。

教育目的

此实现可作为以下方面的学习示例:

  • MCP协议概念 -了解AI代理如何与外部工具通信
  • 清洁建筑 -展示关注点分离和模块化设计
  • AI代理模式 -展示LLM如何协调工具使用
  • 现代Python实践 -FastAPI、依赖注入和容器化

该项目使用JSONPlaceholder API作为简单的外部资源,并使用Ollama的Mistral模型进行人工智能处理,使其易于理解和实验MCP概念。

架构概述

MCP服务器遵循干净的架构原则,明确分离关注点:

系统上下文

C4Context
    title System Context Diagram for MCP Server

    Person(user, "User", "Developer or AI system user")
    
    System(mcpServer, "MCP Server", "AI access to secure internal systems - bridges AI models with external resources")
    
    System_Ext(ollama, "Ollama", "Local LLM service running Mistral model")
    System_Ext(jsonplaceholder, "JSONPlaceholder", "External REST API for testing and prototyping")
    
    Rel(user, mcpServer, "Sends natural language queries", "HTTP/REST")
    Rel(mcpServer, ollama, "Sends prompts and receives AI responses", "HTTP/REST")
    Rel(mcpServer, jsonplaceholder, "Fetches posts and comments", "HTTP/REST")
    
    UpdateRelStyle(user, mcpServer, $textColor="blue", $lineColor="blue")
    UpdateRelStyle(mcpServer, ollama, $textColor="green", $lineColor="green")
    UpdateRelStyle(mcpServer, jsonplaceholder, $textColor="orange", $lineColor="orange")

有关详细的架构图(容器和组件级别),请参阅 C4架构文档.

主要特点

  • 模块化适配器/插件架构 便于扩展
  • 基于环境的配置管理 (.env文件)
  • 依赖注入 为了更好的可测试性
  • LLM代理集成 与Ollama(米斯特拉尔模型)
  • 干净的建筑 关注点分离
  • Docker支持 开发和生产配置分开
  • 快速API 带有自动API文档

项目结构

docker-compose.yml        # Docker Compose configuration with Ollama service
Dockerfile                # Dockerfile for building the app image
requirements.txt          # Python dependencies
.env.local               # Local development configuration
.env.docker              # Docker/production configuration
app/
    config.py             # Configuration management with environment support
    llm_agent.py          # LLM agent integration
    logger.py             # Logging setup
    main.py               # FastAPI application entry point
    router.py             # API routing with dependency injection
    adapters/             # Adapter plugins for external resources
        jsonplaceholder_comments.py
        jsonplaceholder_post.py
    prompts/              # System prompts for LLM agents
        system_prompt.txt
docker/
    ollama-entrypoint.sh  # Automated Ollama setup script
docs/
    c4-architecture.md    # Detailed C4 architecture diagrams

快速开始

地方发展

# Install dependencies
pip install -r requirements.txt

# Install Ollama (if not already installed)
# Download from: https://ollama.ai/download

# Setup Ollama with mistral model
ollama pull mistral
ollama run mistral

# Run the application
uvicorn app.main:app --host 0.0.0.0 --port 8080 --reload --env-file .env.local

Docker部署

# Start all services (includes Ollama with automatic mistral download)
docker-compose up --build

# This will:
    - Start Ollama service and automatically download mistral model
    - Build and start the MCP Server application
    - Set up internal Docker networking between services

API使用

交互式文档

  • 本地: http://127.0.0.1:8080/docs
  • 码头工人: http://localhost:8080/docs

请求示例

让AI代理获取数据:

# Get a post using natural language
curl -X POST http://localhost:8080/ask \
  -H "Content-Type: application/json" \
  -d '{"query":"Get me post number two"}'

# Get comments for that post
curl -X POST http://localhost:8080/ask \
  -H "Content-Type: application/json" \
  -d '{"query":"Now show me all comments for that post"}'

直接工具调用:

# Fetch post directly
curl -X POST http://localhost:8080/post-call \
  -H "Content-Type: application/json" \
  -d '{"post_id": 2}'

# Fetch comments directly
curl -X POST http://localhost:8080/comments-call \
  -H "Content-Type: application/json" \
  -d '{"post_id": 2}'

配置

应用程序使用基于环境的配置:

  • .env.local -本地开发(本地主机URL)
  • .env.docker -Docker环境(容器服务名称)

关键配置选项:

  • llm_base_url -API终点
  • local_api_base -此应用程序的基本URL
  • jsonplaceholder_base_url -外部API端点

体系结构模式

  • 依赖注入 -FastAPI Depends() 设置和记录系统
  • 适配器模式 -具有一致接口的外部API集成
  • 代理模式 -LLM编排和工具调度
  • 清洁建筑 -跨层关注点分离
  • 基于环境的配置 -针对不同环境的不同配置

Docker存储

该应用程序使用Docker卷来保存数据:

  • ollama_data -存储mistral模型(~4GB)和Ollama配置
  • 模型在容器重启之间保持不变,以加快启动时间

故障排除

常见问题:

  1. Docker中的“名称或服务未知”

    • 使用 docker-compose up 一起启动所有服务
    • 确保.env.docker使用正确的服务名称
  2. Ollama连接被拒绝

    • 本地:确保 ollama run mistral 正在运行
    • 码头工人:等待mistral模型下载(检查日志)
  3. 未找到模型错误

    • 本地:运行 ollama pull mistral
    • 码头工人:如果下载失败,请重新启动服务

许可证

该项目根据MIT许可证获得许可。请参阅 许可证 文件以获取详细信息。

发展

该项目是在以下机构的协助下开发的:

  • ChatGPT o4迷你高 -AI对代码架构和实现的帮助
  • GitHub复制品代理(Claude Sonnet 4) -人工智能驱动的代码完成和重构

作者

帕维尔·谢尔科