|
| 1 | +# MCP (Model Context Protocol) Implementation in IntelliNode |
| 2 | + |
| 3 | +## Overview |
| 4 | +Added support for Model Context Protocol (MCP) to IntelliNode. MCP allows IntelliNode to connect to external tools and data sources via MCP servers. This is implemented as a "nice to have" feature with **zero additional dependencies** - only using existing `cross-fetch` package. |
| 5 | + |
| 6 | +## What Was Added |
| 7 | + |
| 8 | +### 1. Core Implementation: `utils/MCPClient.js` |
| 9 | +A lightweight HTTP-based MCP client for connecting to MCP servers. |
| 10 | + |
| 11 | +**Features:** |
| 12 | +- ✅ Connect to MCP servers via HTTP |
| 13 | +- ✅ Fetch available tools from MCP server |
| 14 | +- ✅ Call tools with parameters |
| 15 | +- ✅ Tool discovery and listing |
| 16 | +- ✅ Error handling |
| 17 | +- ✅ **No external dependencies** (uses cross-fetch which is already a dependency) |
| 18 | + |
| 19 | +**Key Methods:** |
| 20 | +```javascript |
| 21 | +class MCPClient { |
| 22 | + constructor(serverUrl) // Initialize with MCP server URL |
| 23 | + async initialize() // Connect and fetch tools |
| 24 | + async getTools() // Get all tools from server |
| 25 | + async callTool(toolName, input) // Execute a tool |
| 26 | + getTool(toolName) // Get specific tool |
| 27 | + getToolNames() // List all tool names |
| 28 | + hasTool(toolName) // Check if tool exists |
| 29 | + listTools() // Get formatted tool list |
| 30 | +} |
| 31 | +``` |
| 32 | +
|
| 33 | +### 2. Test Suite: `test/integration/MCPClient.test.js` |
| 34 | +Comprehensive test coverage with **2 mock tests + 2 optional real server tests**: |
| 35 | +
|
| 36 | +**Mock Tests (Always Run - No Server Needed):** |
| 37 | +- ✅ Test 1: Tool management methods (getToolNames, getTool, hasTool, listTools) |
| 38 | +- ✅ Test 2: Error handling for non-existent tools |
| 39 | +
|
| 40 | +**Real Server Tests (Optional - Requires MCP Server):** |
| 41 | +- ✅ Test 3: Initialize connection with real MCP server |
| 42 | +- ✅ Test 4: Fetch and list tools from real server |
| 43 | +
|
| 44 | +**Run tests:** |
| 45 | +```bash |
| 46 | +# Mock tests only (CI/CD ready, always passes) |
| 47 | +node test/integration/MCPClient.test.js |
| 48 | + |
| 49 | +# With real server (optional) |
| 50 | +MCPClient_REAL_SERVER=true node test/integration/MCPClient.test.js |
| 51 | +``` |
| 52 | +
|
| 53 | +**Setup Real Server Tests:** |
| 54 | +```bash |
| 55 | +# Terminal 1: Start MCP server |
| 56 | +npx @modelcontextprotocol/server-filesystem /tmp |
| 57 | + |
| 58 | +# Terminal 2: Run tests with real server |
| 59 | +MCPClient_REAL_SERVER=true node test/integration/MCPClient.test.js |
| 60 | +``` |
| 61 | +
|
| 62 | +All mock tests pass ✓ |
| 63 | +
|
| 64 | +### 3. Updated Files |
| 65 | +
|
| 66 | +#### `index.js` (Line 78, 138) |
| 67 | +- Added MCPClient import |
| 68 | +- Added MCPClient to module.exports |
| 69 | +
|
| 70 | +#### `package.json` |
| 71 | +- Version bumped: `2.3.0` → `2.4.0` |
| 72 | +- Added keywords: "mcp", "model-context-protocol" |
| 73 | +
|
| 74 | +#### `README.md` |
| 75 | +- Added new "Model Context Protocol (MCP)" section in Utilities |
| 76 | +- Includes example code and reference to MCP documentation |
| 77 | +
|
| 78 | +## No Dependencies Added! 🎉 |
| 79 | +This implementation uses **only existing dependencies**: |
| 80 | +- `cross-fetch` (already in package.json) - for HTTP requests |
| 81 | +- No need for `@modelcontextprotocol/sdk` (can be added later if full SDK features needed) |
| 82 | +- No need for `zod` (not required for basic tool communication) |
| 83 | +
|
| 84 | +## Usage Examples |
| 85 | +
|
| 86 | +### Basic Usage |
| 87 | +```javascript |
| 88 | +const { MCPClient } = require('intellinode'); |
| 89 | + |
| 90 | +// Connect to MCP server |
| 91 | +const mcpClient = new MCPClient('http://localhost:3000'); |
| 92 | + |
| 93 | +// Initialize and get tools |
| 94 | +const tools = await mcpClient.initialize(); |
| 95 | +console.log('Available tools:', mcpClient.getToolNames()); |
| 96 | + |
| 97 | +// Call a tool |
| 98 | +const result = await mcpClient.callTool('get_weather', { |
| 99 | + location: 'New York', |
| 100 | + units: 'celsius' |
| 101 | +}); |
| 102 | +``` |
| 103 | +
|
| 104 | +### With Chatbot Integration |
| 105 | +```javascript |
| 106 | +const { Chatbot, ChatGPTInput } = require('intellinode'); |
| 107 | +const { MCPClient } = require('intellinode'); |
| 108 | + |
| 109 | +// Initialize MCP client |
| 110 | +const mcpClient = new MCPClient('http://localhost:3000'); |
| 111 | +await mcpClient.initialize(); |
| 112 | + |
| 113 | +// Create chat input with tool context |
| 114 | +const input = new ChatGPTInput('You are an assistant with access to tools'); |
| 115 | + |
| 116 | +// Add tools information to context |
| 117 | +const toolsContext = mcpClient.listTools() |
| 118 | + .map(t => `- ${t.name}: ${t.description}`) |
| 119 | + .join('\n'); |
| 120 | + |
| 121 | +input.addSystemMessage(`Available tools:\n${toolsContext}`); |
| 122 | +input.addUserMessage('What is the weather in New York?'); |
| 123 | + |
| 124 | +// Get response from chatbot |
| 125 | +const bot = new Chatbot(openaiKey); |
| 126 | +const responses = await bot.chat(input); |
| 127 | +``` |
| 128 | +
|
| 129 | +### Mock Tools (for testing without server) |
| 130 | +```javascript |
| 131 | +const mcpClient = new MCPClient('http://localhost:3000'); |
| 132 | + |
| 133 | +// Simulate tools locally (useful for development) |
| 134 | +mcpClient.tools = [ |
| 135 | + { |
| 136 | + name: 'tool_name', |
| 137 | + description: 'What this tool does', |
| 138 | + inputSchema: { /* schema */ } |
| 139 | + } |
| 140 | +]; |
| 141 | + |
| 142 | +// Now you can use the client without running a server |
| 143 | +console.log(mcpClient.listTools()); |
| 144 | +``` |
| 145 | +
|
| 146 | +## MCP Servers to Try |
| 147 | +
|
| 148 | +### Built-in Servers (from Anthropic) |
| 149 | +```bash |
| 150 | +# Filesystem server |
| 151 | +npx @modelcontextprotocol/server-filesystem /path/to/root |
| 152 | + |
| 153 | +# GitHub server |
| 154 | +npm install -g @modelcontextprotocol/server-github |
| 155 | + |
| 156 | +# Slack server |
| 157 | +npm install -g @modelcontextprotocol/server-slack |
| 158 | +``` |
| 159 | +
|
| 160 | +### Other Available Servers |
| 161 | +- Google Drive |
| 162 | +- Notion |
| 163 | +- GitLab |
| 164 | +- Linear |
| 165 | +- And many more at: https://modelcontextprotocol.io |
| 166 | +
|
| 167 | +## Architecture |
| 168 | +
|
| 169 | +``` |
| 170 | +┌─────────────────────────────────────┐ |
| 171 | +│ IntelliNode Application │ |
| 172 | +├─────────────────────────────────────┤ |
| 173 | +│ │ |
| 174 | +│ Chatbot + MCPClient │ |
| 175 | +│ │ │ |
| 176 | +└─────────────────────────┼───────────┘ |
| 177 | + │ |
| 178 | + HTTP/JSON-RPC |
| 179 | + │ |
| 180 | + ┌─────▼─────┐ |
| 181 | + │ MCP Server │ |
| 182 | + │ (Tools) │ |
| 183 | + └────────────┘ |
| 184 | +``` |
| 185 | +
|
| 186 | +## Design Decisions |
| 187 | +
|
| 188 | +1. **Minimal Dependencies**: No new npm packages required |
| 189 | + - Only uses existing `cross-fetch` |
| 190 | + - Can upgrade to full SDK if needed later |
| 191 | +
|
| 192 | +2. **Simple HTTP Transport**: |
| 193 | + - Easy to debug |
| 194 | + - Works with any HTTP-based MCP server |
| 195 | + - Can be extended for WebSocket/SSE later |
| 196 | +
|
| 197 | +3. **Flexible Tool Integration**: |
| 198 | + - Tools can be loaded from server or mocked locally |
| 199 | + - Easy to integrate into Chatbot prompts |
| 200 | + - No breaking changes to existing code |
| 201 | +
|
| 202 | +4. **Good to Have Philosophy**: |
| 203 | + - Doesn't affect core library functionality |
| 204 | + - Can be used optionally |
| 205 | + - No required setup or configuration |
| 206 | +
|
| 207 | +## Future Enhancements |
| 208 | +
|
| 209 | +1. **WebSocket/SSE Transport**: For streaming responses |
| 210 | +2. **Tool Caching**: Cache tool definitions to reduce calls |
| 211 | +3. **Retry Logic**: Auto-retry with exponential backoff |
| 212 | +4. **Request Timeout**: Add configurable timeout handling |
| 213 | +5. **Authentication**: Support for API keys and OAuth |
| 214 | +6. **Batch Tool Calls**: Execute multiple tools in parallel |
| 215 | +7. **SDK Integration**: Optional `@modelcontextprotocol/sdk` for full features |
| 216 | +
|
| 217 | +## Testing |
| 218 | +
|
| 219 | +✅ All mock tests pass (CI/CD ready) |
| 220 | +``` |
| 221 | +✓ Test 1: Mock Tool Management Methods |
| 222 | +✓ Test 2: Mock Error Handling |
| 223 | +``` |
| 224 | +
|
| 225 | +Optional real server tests (require running MCP server): |
| 226 | +``` |
| 227 | +✓ Test 3: Real Server - Initialize |
| 228 | +✓ Test 4: Real Server - List Tools |
| 229 | +``` |
| 230 | +
|
| 231 | +**Run tests:** |
| 232 | +```bash |
| 233 | +# Default: Mock tests only |
| 234 | +node test/integration/MCPClient.test.js |
| 235 | + |
| 236 | +# With real server (set environment variable) |
| 237 | +MCPClient_REAL_SERVER=true node test/integration/MCPClient.test.js |
| 238 | + |
| 239 | +# Setup real server before running |
| 240 | +npx @modelcontextprotocol/server-filesystem /tmp |
| 241 | +``` |
| 242 | +
|
| 243 | +## Version Info |
| 244 | +
|
| 245 | +- **IntelliNode Version**: 2.4.0 (increased from 2.3.0) |
| 246 | +- **MCP Implementation**: v1.0 (Basic HTTP client) |
| 247 | +- **Node.js Support**: 14+ |
| 248 | +
|
| 249 | +## References |
| 250 | +
|
| 251 | +- [Model Context Protocol Documentation](https://modelcontextprotocol.io) |
| 252 | +- [MCP GitHub Repository](https://github.com/modelcontextprotocol) |
| 253 | +- [LlamaIndex MCP Integration](https://developers.llamaindex.ai/typescript/framework/modules/agents/tool/#mcp-tools) |
| 254 | +- [OpenAI MCP Support](https://platform.openai.com/docs/mcp) |
| 255 | +- [Anthropic MCP Announcement](https://www.anthropic.com/news/model-context-protocol) |
0 commit comments