AI模型交互协议

Model Context Protocol(MCP)是一个标准化协议,用于AI应用程序与外部工具和数据源的交互,支持AI集成、工具执行、数据访问和上下文保持。

  • by niharikakanakala
  • JavaScript
  • 本地部署
  • 更新于
在 GitHub 查看 niharikakanakala/-nodejs-mcp-server-with-tools-and-resources

使用说明

🛡️ 什么是MCP?

模型上下文协议是人工智能应用程序与外部工具和数据源交互的标准化方式。此服务器支持:

🤖 AI集成:与Claude、ChatGPT和其他AI模型直接集成

🔧 工具执行:AI可以执行计算和数据操作

📊 数据访问:AI可以查询和分析您的数据

🔄 上下文保护:在AI交互中保持状态

🌟 特性

🛠️ 工具(可执行函数)

Calculator Tool: Perform mathematical operations (add, subtract, multiply, divide)
Data Retrieval Tool: Get specific records by ID and type
Data Creation Tool: Create new users, products, and orders
Advanced Validation: Input validation for all tool operations

📁 资源(数据访问)

Users Resource: Access user data collection
Products Resource: Access product catalog
Orders Resource: Access order history
Settings Resource: Server configuration and system settings

💾 数据管理

  In-Memory Data Store: Fast, efficient data storage using Maps
  CRUD Operations: Complete Create, Read, Update, Delete functionality
  Search Capabilities: Query data across all collections
  Statistics Generation: Real-time data analytics and metrics

🏗️ 建筑

MCP服务器组件:

🏭 Factory Pattern → Server Creation & Configuration
🔧 Tools Handler → Executable Functions (calculate, get_data, create_data)
📂 Resources Handler → Data Access (users, products, orders, settings)
💾 Data Service → CRUD Operations & In-Memory Storage
✅ Validation Utilities → Input Validation & Error Handling
🧪 Test Suite → Comprehensive Testing Coverage

🚀 快速开始 先决条件 Node.js 16+ npm或纱线

运行服务器

npm启动

运行测试

npm测试

📋 API终点

httpGET  /              # Welcome message
GET  /health        # Server health check
GET  /mcp/info      # MCP server information

Tools Endpoints

httpGET  /tools/list    # List available tools
POST /tools/call    # Execute a tool

Resources Endpoints

httpGET  /resources/list # List available resources
POST /resources/read # Read from a resource

🔒 安全功能

CORS Protection: Configurable cross-origin resource sharing
Helmet Integration: Security headers for enhanced protection
Input Validation: Comprehensive validation for all inputs
Error Handling: Proper error responses without data leakage
Request Size Limits: Protection against large payload attacks

🚀 性能特点

Factory Pattern: Efficient server instance creation
In-Memory Storage: Lightning-fast data operations
Async/Await: Non-blocking operations throughout
Singleton Services: Optimized memory usage
Modular Architecture: Easy to extend and maintain

📞 支持

📧 问题: GitHub问题

📖 文档: https://modelcontextprotocol.io/docs/concepts/architecture

💬 讨论: GitHub讨论


⭐ 如果这个项目对你有帮助,请在GitHub上给它一颗星!