Skip to main content

MCP server for intelligent knowledge base search and retrieval with Dify integration

Project description

KB-Bridge

Tests Code Coverage

A Model Context Protocol (MCP) server for intelligent knowledge base search and retrieval with support for multiple backend providers.

Installation

pip install kbbridge

Quick Start

Configuration

Create a .env file with your retrieval backend credentials:

# Required - Retrieval Backend Configuration
RETRIEVAL_ENDPOINT=https://api.dify.ai/v1  # Example: Dify endpoint
RETRIEVAL_API_KEY=your-retrieval-api-key
LLM_API_URL=https://your-llm-service.com/v1
LLM_MODEL=gpt-4o
LLM_API_TOKEN=your-token-here

# Optional
RERANK_URL=https://your-rerank-api.com
RERANK_MODEL=your-rerank-model

Supported Backends:

Backend Status Notes
Dify Supported Currently available
Others Planned Additional backends coming soon

See env.example for all available configuration options.

Running the Server

# Start server
python -m kbbridge.server --host 0.0.0.0 --port 5210

# Or using Makefile (if available)
make start

Server runs on http://0.0.0.0:5210 with MCP endpoint at http://0.0.0.0:5210/mcp.

Deployment Options

Option 1: Docker (Local Development / Simple Deployments)

For local development or simple single-container deployments:

# Build the image
docker build -t kbbridge:latest .

# Run with environment variables
docker run -d \
  --name kbbridge \
  -p 5210:5210 \
  --env-file .env \
  kbbridge:latest

For production deployments, use container orchestration platforms like Kubernetes with your preferred deployment method.

Features

  • Backend Integration: Extensible architecture supporting multiple retrieval backends
  • Multiple Search Methods: Hybrid, semantic, keyword, and full-text search
  • Quality Reflection: Automatic answer quality evaluation and refinement
  • Custom Instructions: Domain-specific query guidance

Available Tools

  • assistant: Intelligent search and answer extraction from knowledge bases
  • file_discover: Discover relevant files using retriever + optional reranking
  • file_lister: List files in knowledge base datasets
  • keyword_generator: Generate search keywords using LLM
  • retriever: Retrieve information using various search methods
  • file_count: Get file count in knowledge base dataset

Usage Examples

Basic Query

import asyncio
from fastmcp import Client


async def main():
    async with Client("http://localhost:5210/mcp") as client:
        result = await client.call_tool(
            "assistant",
            {
                "resource_id": "resource-id",
                "query": "What are the safety protocols?",
            },
        )
        print(result.content[0].text)

asyncio.run(main())

With Custom Instructions

await client.call_tool("assistant", {
    "resource_id": "hr_dataset",
    "query": "What is the maternity leave policy?",
    "custom_instructions": "Focus on HR compliance and legal requirements."
})

With Query Rewriting

await client.call_tool("assistant", {
    "resource_id": "resource-id",
    "query": "What are the safety protocols?",
    "enable_query_rewriting": True  # Enables LLM-based query expansion/relaxation
})

With Document Filtering

await client.call_tool("assistant", {
    "resource_id": "resource-id",
    "query": "What are the safety protocols?",
    "document_name": "safety_manual.pdf"  # Limit search to specific document
})

Integration with Dify

You can plug KB-Bridge into a Dify Agent Workflow instead of calling MCP tools directly:

  1. Configure MCP Connection
    • MCP server URL: http://localhost:5210/mcp
    • Add auth headers: X-RETRIEVAL-ENDPOINT, X-RETRIEVAL-API-KEY, X-LLM-API-URL, X-LLM-MODEL
  2. Create an Agent Workflow
    • Add an “MCP Tool” node
    • Select tool: assistant
    • Map workflow variables to resource_id, query, and other tool parameters
  3. Run Queries
    • User input → Agent → MCP assistant tool → Structured answer with citations

Development

# Install development dependencies
pip install -e ".[dev]"

# Run tests
pytest tests/

# Format code
black kbbridge/ tests/

# Lint code
ruff check kbbridge/ tests/

License

Apache-2.0

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

kbbridge-0.2.1.tar.gz (96.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

kbbridge-0.2.1-py3-none-any.whl (123.5 kB view details)

Uploaded Python 3

File details

Details for the file kbbridge-0.2.1.tar.gz.

File metadata

  • Download URL: kbbridge-0.2.1.tar.gz
  • Upload date:
  • Size: 96.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.12

File hashes

Hashes for kbbridge-0.2.1.tar.gz
Algorithm Hash digest
SHA256 4cf14389b5f82ea35257c954df72360486753a8871b093ec10376223f7972f9a
MD5 48396fe3545234b7f054ab69cd28c2a0
BLAKE2b-256 ebb647840c6f90fe167cef6e81ab293ebae850dc80a4ddc6738e94fdd057cba5

See more details on using hashes here.

File details

Details for the file kbbridge-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: kbbridge-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 123.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.12

File hashes

Hashes for kbbridge-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 0920b0748c6e6e974e2541d1dfa06e851ae497582d2c6aa7484dba8e600566c8
MD5 d071b2e2f8bf17aae33d3e04384c2287
BLAKE2b-256 3786becba547f7e2b9defe0756193ec5b87b98f9807250bfd480a84ffc888456

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page