{"id":33295181,"url":"https://github.com/rocket-connect/mcp-rag","last_synced_at":"2026-05-06T13:32:35.328Z","repository":{"id":324740974,"uuid":"1095479461","full_name":"rocket-connect/mcp-rag","owner":"rocket-connect","description":"A lightweight wrapper around AI SDK that intelligently indexes and retrieves MCP tools using graph-based vector search.","archived":false,"fork":false,"pushed_at":"2025-11-17T15:28:07.000Z","size":1176,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-11-17T17:20:19.530Z","etag":null,"topics":["ai","augmented","characters","embeddings","generative-ai","limits","mcp","neo4j","performance","rag","scale","tools","vector-search"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/rocket-connect.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null},"funding":{"github":"rocket-connect"}},"created_at":"2025-11-13T05:25:12.000Z","updated_at":"2025-11-17T15:28:56.000Z","dependencies_parsed_at":null,"dependency_job_id":null,"html_url":"https://github.com/rocket-connect/mcp-rag","commit_stats":null,"previous_names":["rocket-connect/mcp-rag"],"tags_count":null,"template":false,"template_full_name":null,"purl":"pkg:github/rocket-connect/mcp-rag","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rocket-connect%2Fmcp-rag","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rocket-connect%2Fmcp-rag/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rocket-connect%2Fmcp-rag/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rocket-connect%2Fmcp-rag/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rocket-connect","download_url":"https://codeload.github.com/rocket-connect/mcp-rag/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rocket-connect%2Fmcp-rag/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":284988465,"owners_count":27095952,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-11-18T02:00:05.759Z","response_time":61,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ai","augmented","characters","embeddings","generative-ai","limits","mcp","neo4j","performance","rag","scale","tools","vector-search"],"created_at":"2025-11-18T02:01:08.410Z","updated_at":"2026-05-06T13:32:35.321Z","avatar_url":"https://github.com/rocket-connect.png","language":"TypeScript","funding_links":["https://github.com/sponsors/rocket-connect"],"categories":[],"sub_categories":[],"readme":"# MCP RAG\n\n[![npm version](https://badge.fury.io/js/@mcp-rag%2Fclient.svg)](https://badge.fury.io/js/@mcp-rag%2Fclient) [![CI](https://github.com/rocket-connect/mcp-rag/actions/workflows/ci.yml/badge.svg)](https://github.com/rocket-connect/mcp-rag/actions/workflows/ci.yml) [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)\n\nA lightweight wrapper around AI SDK that intelligently indexes and retrieves MCP tools using graph-based vector search.\n\n\u003cdiv align=\"center\"\u003e\n  \u003cimg src=\"/docs/neo4j-model.png\" alt=\"Neo4j Model\" width=\"60%\" /\u003e\n\u003c/div\u003e\n\n## What It Does\n\nMCP RAG indexes your MCP toolset into a graph structure and uses Neo4j-powered vector search to retrieve relevant tool subsets from large collections. This dramatically reduces context overhead when working with extensive tool libraries.\n\n## Benchmarks\n\nMCP RAG sees improvements in both efficiency and performance compared to baseline tool selection, while maintaining the same level of accuracy.\n\n**Benchmark Methodology:** Tests simulate a realistic conversation with 5 sequential prompts, each triggering a different tool as context accumulates—mirroring real-world multi-turn interactions. All tests use the complete toolset from the GitHub MCP Server (90+ tools) to represent authentic large-scale tool selection scenarios.\n\nSee the proof in the pudding 🍰:\n\n**[Base Tool Selection Results](./benchmarks/results/base-tool-selection/latest.md)** - Baseline approach passing all tools to the model.\n\n**[RAG Tool Selection Results](./benchmarks/results/rag-tool-selection/latest.md)** - RAG-powered intelligent filtering with vector search.\n\n**[View Test Suite](./benchmarks/src/rag.test.ts)** - Complete benchmark implementation and test cases.\n\n## Installation\n\n[![npm version](https://badge.fury.io/js/@mcp-rag%2Fclient.svg)](https://badge.fury.io/js/@mcp-rag%2Fclient)\n\n```bash\nnpm install @mcp-rag/client @ai-sdk/openai neo4j-driver ai\n```\n\nSet your OpenAI API key:\n\n```bash\nexport OPENAI_API_KEY=your_key_here\n```\n\n## Quick Start\n\n```typescript\nimport { createMCPRag } from '@mcp-rag/client'\nimport { openai } from '@ai-sdk/openai'\nimport neo4j from 'neo4j-driver'\nimport { tool } from 'ai'\nimport { z } from 'zod'\n\nconst driver = neo4j.driver(\n  'neo4j://localhost:7687',\n  neo4j.auth.basic('neo4j', 'password')\n)\n\nconst rag = createMCPRag({\n  model: openai('gpt-4o-mini'),\n  openaiApiKey: process.env.OPENAI_API_KEY || '',\n  neo4j: driver,\n  tools: {\n    searchDocs: tool({\n      /* ... */\n    }),\n    queryDatabase: tool({\n      /* ... */\n    }),\n    sendEmail: tool({\n      /* ... */\n    }),\n    fetchWeather: tool({\n      /* ... */\n    }),\n    analyzeImage: tool({\n      /* ... */\n    }),\n    // ... hundreds more tools\n  },\n})\n\nawait rag.sync()\nconst result = await rag.generateText({\n  prompt: 'Search for API docs',\n})\n```\n\n\u003cdetails\u003e\n\u003csummary\u003e\u003cstrong\u003eWhat does \u003ccode\u003erag.sync()\u003c/code\u003e do?\u003c/strong\u003e\u003c/summary\u003e\n\n\u003cbr\u003e\n\nThe `sync()` method performs a complete synchronization of your tools to Neo4j, creating the graph structure needed for semantic search. Here's what happens under the hood:\n\n1. **Creates Vector Index**: Sets up a Neo4j vector index for similarity search using 1536-dimensional embeddings (OpenAI's `text-embedding-3-small` model)\n\n2. **Generates Embeddings**: For each tool in your toolset, it creates embeddings for:\n   - The tool itself (name + description)\n   - Each parameter (name + description)\n   - The return type\n\n3. **Builds Graph Structure**: Creates a graph in Neo4j with the following relationships:\n   - `ToolSet` nodes that group tools together\n   - `Tool` nodes with their embeddings\n   - `Parameter` nodes connected to tools via `HAS_PARAM` relationships\n   - `ReturnType` nodes connected to tools via `RETURNS` relationships\n\n4. **Idempotent by Design**: The sync process uses `MERGE` operations, so running it multiple times won't create duplicates. It will update existing nodes if the toolset has changed.\n\n**When to call it:**\n\n- After initial client creation (required before first use)\n- After adding or removing tools with `addTool()` or `removeTool()`\n- To force a re-index of your tools\n\nThe sync process is optimized to only run when necessary - subsequent calls to `generateText()` won't re-sync unless you explicitly call `sync()` again or modify the toolset.\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003e\u003cstrong\u003eWhat does \u003ccode\u003erag.generateText()\u003c/code\u003e do?\u003c/strong\u003e\u003c/summary\u003e\n\n\u003cbr\u003e\n\nThe `generateText()` method is a smart wrapper around the AI SDK's `generateText` function that adds automatic tool selection. Here's the workflow:\n\n1. **Ensures Migration**: Automatically calls the sync process if tools haven't been indexed yet\n\n2. **Semantic Tool Selection**:\n   - Generates an embedding for your prompt\n   - Performs a Neo4j vector similarity search to find the most relevant tools\n   - By default, selects up to 10 tools (configurable via `maxActiveTools`)\n   - You can override this by passing `activeTools` array explicitly\n\n3. **Calls AI SDK**: Passes only the selected subset of tools to the AI SDK's native `generateText` function along with your prompt and any additional options\n\n4. **Returns Full Result**: Returns the complete AI SDK result wrapped in a `GenerateTextResultWrapper` object, giving you access to:\n   - Tool calls made by the model\n   - Token usage statistics\n   - Response content\n   - All other AI SDK metadata\n\n**Key Benefits:**\n\n- **Reduced Context Size**: Only relevant tools are sent to the LLM, saving tokens\n- **Better Performance**: Fewer tools mean faster response times\n- **Same AI SDK Experience**: Accepts all standard AI SDK parameters and returns familiar result structures\n\n\u003c/details\u003e\n\n\u003cdiv align=\"center\"\u003e\n  \u003cimg src=\"/docs/example-tools-model.png\" alt=\"Tools Select Model\" width=\"60%\" /\u003e\n\u003c/div\u003e\n\n## Examples\n\n### GitHub MCP Server Demo\n\nWant to see MCP RAG in action? Check out our complete example that demonstrates intelligent tool selection with the GitHub MCP Server's 93 tools:\n\n**[📖 View GitHub Example →](./examples/github/README.md)**\n\n\u003cdiv align=\"center\"\u003e\n  \u003cimg src=\"/docs/neo4j-browser-tools.png\" alt=\"GitHub Tools in Neo4j Browser\" width=\"60%\" /\u003e\n\u003c/div\u003e\n\nThis example shows:\n\n- How to mock and index all 93 GitHub MCP server tools\n- Vector similarity search selecting the top 10 most relevant tools\n- Real-world tool selection with detailed debug output\n- Interactive testing with different prompts\n\nPerfect for understanding how MCP RAG reduces context overhead in large toolsets!\n\n## Features\n\n- **Graph-based indexing** – Tools are indexed with their relationships and metadata\n- **Vector search** – Neo4j-powered semantic search for tool retrieval\n- **AI SDK compatible** – Drop-in wrapper that works with your existing AI SDK setup\n- **Selective loading** – Only load the tools you need for each request\n\n## API Reference\n\n### `createMCPRag(config)`\n\nCreates an MCP RAG client.\n\n```typescript\nconst rag = createMCPRag({\n  model: LanguageModel,              // AI SDK model (required)\n  neo4j: Driver,                     // Neo4j driver (required)\n  tools: Record\u003cstring, Tool\u003e,       // AI SDK tools (required)\n  openaiApiKey: string,              // For embeddings (required)\n  maxActiveTools?: number,           // Default: 10\n  hashFunction?: (input: string) =\u003e string,  // Custom hash function\n  dangerouslyAllowBrowser?: boolean, // Enable browser usage\n  migration?: {\n    shouldMigrate?: (session) =\u003e Promise\u003cboolean\u003e,\n    migrate?: (session, tools) =\u003e Promise\u003cvoid\u003e,\n    onBeforeMigrate?: (statements) =\u003e Promise\u003cstatements\u003e,\n  }\n})\n```\n\n### Client Methods\n\n| Method                      | Description                                             |\n| --------------------------- | ------------------------------------------------------- |\n| `sync(options?)`            | Sync tools to Neo4j. Returns `{ hash: string }`         |\n| `generateText(options)`     | Generate text with semantic tool selection              |\n| `getActiveTools(options)`   | Get semantically selected tools without generating text |\n| `addTool(name, tool)`       | Add a tool (updates hash, requires re-sync)             |\n| `removeTool(name)`          | Remove a tool (updates hash, requires re-sync)          |\n| `getTools()`                | Get all registered tools                                |\n| `getToolsetHash()`          | Get current toolset hash                                |\n| `getToolsetByHash(hash)`    | Retrieve toolset info from Neo4j                        |\n| `deleteToolsetByHash(hash)` | Delete a toolset from Neo4j                             |\n\n### `getActiveTools(options)`\n\nGet semantically selected tools to use with your own AI SDK calls:\n\n```typescript\nconst { tools, names } = await rag.getActiveTools({\n  prompt: 'What is the weather?',\n  maxTools: 5, // optional, defaults to maxActiveTools\n})\n\n// Use with AI SDK directly\nconst result = await generateText({\n  model: openai('gpt-4o'),\n  tools,\n  prompt: 'What is the weather?',\n})\n```\n\n## Toolset Hashes\n\nHashes uniquely identify toolset versions for change detection and multi-version support.\n\n### How Hashes Work\n\n1. **Serialization** – Tools are deep-cloned (excluding `execute` functions)\n2. **Sorting** – Tools sorted by name, all nested keys sorted recursively\n3. **Hashing** – JSON string passed to hash function\n\n```typescript\n// Hash changes when tools change\nconst hash1 = rag.getToolsetHash()\nrag.addTool('newTool', myTool)\nconst hash2 = rag.getToolsetHash() // Different from hash1\n```\n\n### Default Hash\n\nUses a bitwise hash returning `toolset-\u003chex\u003e`:\n\n```typescript\nconst hash = rag.getToolsetHash() // \"toolset-a1b2c3d4\"\n```\n\n### Custom Hash Function\n\nFor browser environments or custom requirements:\n\n```typescript\nconst rag = createMCPRag({\n  // ...\n  hashFunction: input =\u003e {\n    // Web Crypto API example\n    const encoder = new TextEncoder()\n    const data = encoder.encode(input)\n    const hashBuffer = await crypto.subtle.digest('SHA-256', data)\n    return Array.from(new Uint8Array(hashBuffer))\n      .map(b =\u003e b.toString(16).padStart(2, '0'))\n      .join('')\n  },\n})\n```\n\n### Hash Properties\n\n- **Deterministic** – Same toolset always produces same hash\n- **Order-independent** – Tool/property order doesn't affect hash\n- **Change-sensitive** – Any definition change produces different hash\n\n## Migrations\n\nMigration syncs tool definitions to Neo4j, creating the graph structure for vector search.\n\n### Basic Migration\n\n```typescript\nawait rag.sync() // Migrates if needed\n```\n\n### Migration Flow\n\n1. **Check** – Determines if migration needed (hash exists in Neo4j?)\n2. **Index** – Creates vector index (1536 dimensions, cosine similarity)\n3. **Embed** – Generates embeddings for tools, parameters, return types\n4. **Store** – Creates graph structure in Neo4j\n\n### Custom Migration Hooks\n\n```typescript\nconst rag = createMCPRag({\n  // ...\n  migration: {\n    // Override migration check\n    shouldMigrate: async session =\u003e {\n      const result = await session.run('...')\n      return result.records.length === 0\n    },\n\n    // Custom migration logic\n    migrate: async (session, tools) =\u003e {\n      // Your migration code\n    },\n\n    // Intercept/modify migration statements\n    onBeforeMigrate: async statements =\u003e {\n      console.log('Migrating:', statements.length, 'statements')\n      return statements // Return modified or original\n    },\n  },\n})\n```\n\n### Multi-Version Toolsets\n\nMultiple toolset versions can coexist in Neo4j:\n\n```typescript\n// Version 1\nawait rag.sync()\nconst v1Hash = rag.getToolsetHash()\n\n// Version 2 (both exist in DB)\nrag.addTool('newTool', myTool)\nawait rag.sync()\nconst v2Hash = rag.getToolsetHash()\n\n// Manage versions\nconst v1Info = await rag.getToolsetByHash(v1Hash)\nawait rag.deleteToolsetByHash(v1Hash) // Clean up old version\n```\n\n### Toolset Lifecycle\n\n```typescript\n// 1. Setup\nconst { hash } = await rag.sync()\n\n// 2. Retrieve\nconst info = await rag.getToolsetByHash(hash)\n// { hash, updatedAt, toolCount, tools: [...] }\n\n// 3. Update\nrag.addTool('newTool', tool)\nawait rag.sync() // Creates new version\n\n// 4. Cleanup\nawait rag.deleteToolsetByHash(oldHash)\n```\n\n## License\n\nMIT [rconnect.tech](https://www.rconnect.tech)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frocket-connect%2Fmcp-rag","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frocket-connect%2Fmcp-rag","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frocket-connect%2Fmcp-rag/lists"}