MCP (Model Context Protocol) Server for AI Cogence RAG Backend
Project description
MCP Server for AI Cogence Backend
Overview
This MCP (Model Context Protocol) server exposes the AI Cogence backend's RAG capabilities, knowledge base, and chat functionality to AI assistants like Claude. It allows AI tools to directly query your knowledge base, perform semantic searches, and manage chat sessions.
Features
๐ ๏ธ Tools Available
-
rag_query- Query the RAG system with natural language questions- Retrieves relevant documents from the knowledge base
- Generates AI-powered answers using retrieved context
- Maintains conversation continuity with session IDs
-
semantic_search- Perform vector similarity search- Find the most relevant text chunks based on embeddings
- Configurable number of results (top_k)
- Returns similarity scores
-
load_markdown_content- Index markdown files into the knowledge base- Loads content from the
/contentdirectory - Makes content searchable through RAG queries
- Updates embeddings automatically
- Loads content from the
-
list_chat_sessions- View all chat sessions- Lists sessions with metadata
- Shows creation and update timestamps
- Configurable limit
-
get_session_messages- Retrieve conversation history- Get all messages from a specific session
- Full conversation context
- Chronological ordering
-
search_knowledge_base- Keyword-based search- Search by text content
- Filter by source document
- Metadata-aware queries
-
get_analytics- System usage statistics- Query counts and session metrics
- Time-range filtering (today, week, month, all)
- Performance insights
๐ Resources Available
-
Content Resources (
cogence://content/*)- All markdown files from
/backend/content/ - Includes: home, about-us, services, etc.
- Format: Markdown
- All markdown files from
-
Knowledge Base (
cogence://knowledge-base)- Complete statistics about stored documents
- Total chunks, sources, and sessions
- Format: JSON
-
Chat History (
cogence://chat-history)- Recent chat sessions (last 20)
- Session IDs and timestamps
- Format: JSON
Installation
1. Install MCP Python SDK
cd /Users/infoobjects/Documents/Projects/ai-cogence-web/backend
source venv/bin/activate
pip install mcp
2. Verify Installation
python mcp_server.py --help
3. Configure Claude Desktop (Optional)
Add to your Claude Desktop configuration file:
macOS: ~/Library/Application Support/Claude/claude_desktop_config.json
{
"mcpServers": {
"ai-cogence-backend": {
"command": "python",
"args": [
"/Users/infoobjects/Documents/Projects/ai-cogence-web/backend/mcp_server.py"
],
"env": {
"PYTHONPATH": "/Users/infoobjects/Documents/Projects/ai-cogence-web/backend"
}
}
}
}
Usage
Running the MCP Server
cd /Users/infoobjects/Documents/Projects/ai-cogence-web/backend
source venv/bin/activate
python mcp_server.py
The server runs on stdio and communicates via JSON-RPC 2.0.
Example Tool Calls
1. Query the RAG System
{
"tool": "rag_query",
"arguments": {
"question": "What services does AI Cogence offer?",
"session_id": "optional-session-id"
}
}
Response:
{
"answer": "AI Cogence offers RAG implementation, AI strategy consulting, fine-tuning services...",
"sources": [
{"content": "...", "metadata": {...}},
{"content": "...", "metadata": {...}}
],
"session_id": "...",
"timestamp": "2025-10-08T10:30:00"
}
2. Semantic Search
{
"tool": "semantic_search",
"arguments": {
"query": "machine learning consulting",
"top_k": 5
}
}
Response:
{
"query": "machine learning consulting",
"results": [
{
"content": "Our AI strategy consulting helps businesses...",
"metadata": {"source": "ai-strategy-consulting.md"},
"similarity": 0.89
},
...
],
"count": 5
}
3. Load Content
{
"tool": "load_markdown_content",
"arguments": {
"collection_name": "cogence_content"
}
}
4. Get Analytics
{
"tool": "get_analytics",
"arguments": {
"time_range": "week"
}
}
Response:
{
"time_range": "week",
"total_queries": 245,
"unique_sessions": 87,
"timestamp": "2025-10-08T10:30:00"
}
Example Resource Access
Read Knowledge Base Stats
URI: cogence://knowledge-base
Response:
{
"total_chunks": 1234,
"total_sources": 45,
"total_sessions": 87,
"timestamp": "2025-10-08T10:30:00"
}
Read Content File
URI: cogence://content/about-us.md
Response: (Full markdown content)
Integration with Claude
Once configured, Claude can automatically:
- Answer questions about your business using the RAG system
- Search your knowledge base for specific information
- Access conversation history for context
- View analytics about system usage
- Read content files directly
Example Claude Interaction
User: "What do you know about AI Cogence's services?"
Claude: (Internally calls rag_query tool)
"AI Cogence offers several services including RAG implementation, AI strategy consulting, and fine-tuning services for enterprises..."
User: "Show me recent chat sessions"
Claude: (Internally calls list_chat_sessions tool)
"Here are the recent chat sessions: [lists sessions with timestamps]"
Architecture
โโโโโโโโโโโโโโโโโโโโโโโ
โ AI Assistant โ
โ (Claude/etc) โ
โโโโโโโโโโโโฌโโโโโโโโโโโ
โ MCP Protocol
โ (JSON-RPC)
โโโโโโโโโโโโผโโโโโโโโโโโ
โ MCP Server โ
โ mcp_server.py โ
โโโโโโโโโโโโโโโโโโโโโโโค
โ โข Tools โ
โ โข Resources โ
โ โข Prompts โ
โโโโโโโโโโโโฌโโโโโโโโโโโ
โ
โโโโโโโโโโโโผโโโโโโโโโโโ
โ Backend Services โ
โโโโโโโโโโโโโโโโโโโโโโโค
โ โข RAG Engine โ
โ โข LangGraph โ
โ โข PostgreSQL/pgvectorโ
โ โข OpenAI Embeddings โ
โโโโโโโโโโโโโโโโโโโโโโโ
Logging
The server logs to stdout with INFO level by default. Key events logged:
- Server startup/shutdown
- Tool invocations
- Database queries
- Errors and exceptions
View logs:
python mcp_server.py 2>&1 | tee mcp_server.log
Troubleshooting
Database Connection Issues
# Check if PostgreSQL is running
brew services list | grep postgres
# Test database connection
psql -h localhost -p 5432 -U $(whoami) -d postgres
Import Errors
# Ensure virtual environment is activated
source venv/bin/activate
# Install missing dependencies
pip install -r requirements.txt
pip install mcp
Port Conflicts
The MCP server uses stdio, not network ports, so no port conflicts should occur.
Security Considerations
- Database Access: Server requires database credentials in
.env - API Keys: OpenAI API keys must be configured
- Network: stdio-based, no network exposure
- Authentication: Inherits from backend authentication
Development
Adding New Tools
@mcp_server.list_tools()
async def list_tools() -> list[Tool]:
return [
# ... existing tools ...
Tool(
name="my_new_tool",
description="Description of what it does",
inputSchema={
"type": "object",
"properties": {
"param": {"type": "string", "description": "Parameter description"}
},
"required": ["param"]
}
)
]
@mcp_server.call_tool()
async def call_tool(name: str, arguments: Any) -> Sequence[TextContent]:
if name == "my_new_tool":
return await handle_my_new_tool(arguments)
Adding New Resources
@mcp_server.list_resources()
async def list_resources() -> list[Resource]:
return [
# ... existing resources ...
Resource(
uri="cogence://my-resource",
name="My Resource",
mimeType="application/json",
description="Description of the resource"
)
]
Environment Variables
Required in .env:
# Database
DATABASE_URL=postgresql://user:pass@localhost:5432/dbname
# OpenAI
OPENAI_API_KEY=sk-...
# Optional
LOG_LEVEL=INFO
Performance
- Latency: ~100-500ms for RAG queries
- Throughput: Depends on PostgreSQL and OpenAI rate limits
- Caching: Embeddings are cached in database
- Scaling: Horizontal scaling via multiple server instances
Support
For issues or questions:
- Check logs:
python mcp_server.py 2>&1 | grep ERROR - Review backend logs:
tail -f backend.log - Test direct API:
curl http://localhost:5025/health
License
Same as parent project.
Version History
- v1.0.0 (2025-10-08): Initial release
- 7 tools implemented
- 3 resource types
- Full RAG integration
- PostgreSQL/pgvector support
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file ai_cogence_mcp_server-1.0.0.tar.gz.
File metadata
- Download URL: ai_cogence_mcp_server-1.0.0.tar.gz
- Upload date:
- Size: 28.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
47110729e2b71757a0ce6f22299ef1b383633c2b69fbf1f6c0cb3366cf2af124
|
|
| MD5 |
5505fa58dfa88733ac05d890fa77e5dd
|
|
| BLAKE2b-256 |
830b2cae43555fd95f9fc139a3608c4d25a16bf97dee2ad32210c060f1e237b0
|
File details
Details for the file ai_cogence_mcp_server-1.0.0-py3-none-any.whl.
File metadata
- Download URL: ai_cogence_mcp_server-1.0.0-py3-none-any.whl
- Upload date:
- Size: 12.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ed9ad2bc41c2d407df6123d0e1464d433a16bb01bfb4bd46117dca2f7c478f3e
|
|
| MD5 |
5d64183f2f7c3424a76cdfab3c637d1a
|
|
| BLAKE2b-256 |
94b0efdebc98544fd8c8c3a4ff3ea8df72e13e6f6c53aa6109d8621d1f0bd9cc
|