Skip to main content

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

Workflow

KB-Bridge follows a multi-stage pipeline to ensure high-quality answers:

flowchart LR
    Start([User Query]) --> Preprocess[Query Preprocessing<br/>Rewriting & Understanding]

    Preprocess --> FileDiscovery[File Discovery<br/>Find Relevant Files]

    FileDiscovery --> Search[Search Stages]

    Search --> Direct[Direct Approach<br/>Simple Retrieval]
    Search --> Advanced[Advanced Approach<br/>File-level Processing]

    Direct --> Candidates
    Advanced --> Candidates[Collect Candidates]

    Candidates --> Synthesis[Answer Synthesis<br/>Rerank & Format]

    Synthesis --> Reflection{Reflection<br/>Enabled?}
    Reflection -->|Yes| Reflect[Quality Check<br/>& Refinement]
    Reflection -->|No| Final
    Reflect --> Final([Final Answer])

    style Start fill:#e1f5ff
    style Final fill:#c8e6c9
    style FileDiscovery fill:#fff9c4
    style Direct fill:#fff9c4
    style Advanced fill:#fff9c4
    style Reflect fill:#ffccbc
    style Synthesis fill:#e1bee7

Stage Details

Query Preprocessing (Optional)

  • Query Rewriting: LLM-based expansion/relaxation to improve recall
  • Query Understanding: Extract intent and decompose complex queries

File Discovery

  • Semantic search to identify relevant files (recall-focused)
  • Optional quality evaluation with automatic search expansion if quality is low

Search Stages (Parallel)

  • Direct Approach: Simple query → retrieval → answer extraction (fallback)
  • Advanced Approach: File-level processing with content boosting for precision

Answer Synthesis

  • Rerank candidates by relevance (if reranking service available)
  • Combine and deduplicate using LLM

Quality Reflection (Optional)

  • Evaluate answer quality and refine if needed (up to max_iterations)

Implementation Status

The orchestrator (DatasetProcessor) currently implements stages 1-3, 5-8. File Discovery Quality Evaluation (stage 4) is implemented but not yet integrated into the pipeline. See .doc/FILE_DISCOVERY_EVALUATION_CONFIG.md for details.

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

Metadata

Release files for kbbridge 0.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for kbbridge 0.3.0
File Size Uploaded Provenance
kbbridge-0.3.0.tar.gz 101.9 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for kbbridge 0.3.0
File Interpreter ABI Platform
kbbridge-0.3.0-py3-none-any.whl Python 3 none any Details

Total release size: 230.3 kB

Release files / kbbridge-0.3.0.tar.gz

Download URL kbbridge-0.3.0.tar.gz
Size 101.9 kB
Tags Source
SHA-256 checksum
How to use checksums
58feb3c52cc05a48131a0ccdfabc9a271c1c4c49461aa5f2a1fc1fcec226294e
BLAKE2b-256 checksum
How to use checksums
c2ff6a91df78fc2d982f4f6f45900daeca3d27c752c07f31a65a89c83e921d7f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.12

Release files / kbbridge-0.3.0-py3-none-any.whl

Download URL kbbridge-0.3.0-py3-none-any.whl
Size 128.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
993612e7f7dca67ee59bf5c8f461343f40f33f88119f718b31900e54508932ac
BLAKE2b-256 checksum
How to use checksums
bd232993b7e694e0fd7397c6a8ca13db0ec48b2528a074e1b65fb35da0c9b8a2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.12

Security for kbbridge 0.3.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page