This guide explains how to integrate CocoIndex with Claude Code using the Model Context Protocol (MCP).
The Model Context Protocol (MCP) is a standard for connecting AI assistants like Claude to external tools and data sources. It allows Claude Code to:
- Search your documents semantically
- Index new documents
- Retrieve metadata
- Access your knowledge base directly
The Claude Code configuration file is located at:
- macOS/Linux:
~/.claude.json - Windows:
%USERPROFILE%\.claude.json
Add the following to your ~/.claude.json:
{
"mcpServers": {
"cocoindex": {
"command": "/path/to/cocoindex-claude-code/.venv/bin/python",
"args": ["/path/to/cocoindex-claude-code/mcp_server.py"],
"env": {
"COCOINDEX_DATABASE_URL": "postgres://cocoindex:cocoindex@localhost/cocoindex",
"OPENAI_API_KEY": "your-openai-key-here"
}
}
}
}Important: Always use absolute paths, not relative paths.
To find your paths:
cd cocoindex-claude-code
echo "Python: $(pwd)/.venv/bin/python"
echo "Script: $(pwd)/mcp_server.py"After updating the configuration:
# Exit Claude Code
# Press Ctrl+C or type /exit
# Restart
claudeOnce connected, Claude Code has access to these tools:
Search indexed documents using semantic similarity.
Parameters:
| Parameter | Type | Required | Description |
|---|---|---|---|
query |
string | Yes | Search query |
limit |
integer | No | Max results (default: 5) |
Example usage in Claude Code:
> Search my documents for API authentication patterns
Trigger re-indexing of all documents.
Parameters: None
Example:
> Re-index my documents
List all documents currently in the index.
Parameters: None
Example:
> What documents are in my knowledge base?
Get LLM-extracted metadata for a specific document.
Parameters:
| Parameter | Type | Required | Description |
|---|---|---|---|
filename |
string | Yes | Document filename |
Example:
> Get the metadata for README.md
Add a new document to the index.
Parameters:
| Parameter | Type | Required | Description |
|---|---|---|---|
filename |
string | Yes | Filename for the document |
content |
string | Yes | Document content |
Example:
> Add this note to my knowledge base as "meeting-notes.md"
sequenceDiagram
participant U as You
participant CC as Claude Code
participant MCP as MCP Server
participant CI as CocoIndex
participant DB as PostgreSQL
U->>CC: "Find docs about authentication"
Note over CC: Claude decides to use<br/>cocoindex_search tool
CC->>MCP: tools/call: cocoindex_search
Note over MCP: JSON-RPC over stdio
MCP->>CI: search("authentication", limit=5)
CI->>DB: Vector similarity query
DB-->>CI: Matching chunks
CI-->>MCP: QueryOutput
MCP-->>CC: JSON results
CC-->>U: "I found 3 relevant documents..."
The MCP server uses JSON-RPC 2.0 over stdio (standard input/output).
{"jsonrpc": "2.0", "id": 1, "method": "initialize", "params": {}}Response:
{
"jsonrpc": "2.0",
"id": 1,
"result": {
"protocolVersion": "2024-11-05",
"serverInfo": {"name": "cocoindex-mcp", "version": "1.0.0"},
"capabilities": {"tools": {}}
}
}{"jsonrpc": "2.0", "id": 2, "method": "tools/list", "params": {}}{
"jsonrpc": "2.0",
"id": 3,
"method": "tools/call",
"params": {
"name": "cocoindex_search",
"arguments": {"query": "authentication", "limit": 5}
}
}Test the MCP server manually:
cd cocoindex-claude-code
source .venv/bin/activate
echo '{"jsonrpc":"2.0","id":1,"method":"initialize","params":{}}' | python mcp_server.pyYou should see a JSON response.
- Check that paths are absolute, not relative
- Verify the Python path exists:
ls -la /path/to/.venv/bin/python - Ensure the script path exists:
ls -la /path/to/mcp_server.py
- Make sure PostgreSQL is running:
docker compose -f docker/compose.yaml ps - Start if needed:
docker compose -f docker/compose.yaml up -d
- Index your documents first:
cocoindex update main.py - Check that documents exist:
ls data/documents/
Add to your environment:
export COCOINDEX_LOG_LEVEL=DEBUGThe MCP server needs access to your API keys. You can provide them via:
- Environment variables in config:
{
"env": {
"OPENAI_API_KEY": "sk-..."
}
}- System environment:
export OPENAI_API_KEY="sk-...".envfile in the project directory
The default configuration uses a local PostgreSQL instance with basic credentials. For production:
- Use strong passwords
- Restrict network access
- Consider encryption at rest
{
"env": {
"COCOINDEX_DATABASE_URL": "postgres://user:pass@host:port/dbname"
}
}You can run multiple instances with different namespaces:
{
"mcpServers": {
"cocoindex-work": {
"command": "...",
"env": {
"COCOINDEX_APP_NAMESPACE": "work"
}
},
"cocoindex-personal": {
"command": "...",
"env": {
"COCOINDEX_APP_NAMESPACE": "personal"
}
}
}
}