Skip to content

Context Portal 实现原理 #141

@Pines-Cheng

Description

@Pines-Cheng

Context Portal MCP (ConPort) - 一个基于数据库的模型上下文协议(MCP)服务器,主要用于管理结构化的项目上下文。

主要功能

ConPort 是你项目的"记忆银行",它帮助AI助手更好地理解你的特定软件项目,通过结构化的方式存储重要信息,如:

  • 项目决策、进度和系统设计
  • 自定义项目数据(如术语表或规范)
  • 智能搜索项目相关信息
  • RAG支持(检索增强生成)让AI能基于项目上下文给出更准确的回答

核心特性

  1. 结构化存储:使用SQLite数据库(每个工作空间一个数据库)
  2. 多工作空间支持:通过workspace_id管理
  3. 语义搜索:支持向量数据存储和语义搜索
  4. 项目知识图谱:构建项目特定的知识图谱,捕获实体间的关系
  5. MCP集成:作为MCP服务器与各种IDE和AI助手集成

支持的IDE和工具

  • Roo Code
  • CLine
  • Windsurf Cascade
  • Cursor IDE

主要工具类别

  • 产品上下文管理
  • 活跃上下文管理
  • 决策日志
  • 进度跟踪
  • 系统模式管理
  • 自定义数据管理
  • 上下文链接
  • 导入/导出功能

这个工具的目标是替代基于文件的上下文管理系统,提供更可靠和可查询的数据库后端,让AI助手能够访问精确、最新的项目信息,从而提供更有上下文感知的准确回答。

Context Portal (ConPort) 确实是一个 RAG 平台。

RAG 核心功能

ConPort 专门设计为支持检索增强生成 (Retrieval Augmented Generation) 的后端系统: 1

  • 项目知识图谱构建:存储决策、进度、系统模式等结构化项目上下文 2
  • 向量嵌入和语义搜索:为关键文本内容生成向量嵌入,支持基于语义相似性的搜索 3
  • RAG 后端服务:为 AI 助手提供精确、可查询的项目记忆 4

多层次检索能力

ConPort 实现了多种搜索策略来支持不同的 RAG 用例: 5

  • 全文搜索 (FTS):使用 SQLite FTS5 进行关键词匹配
  • 语义搜索:通过 ChromaDB 向量存储进行概念相似性搜索 6
  • 知识图谱遍历:通过 get_linked_items 工具探索实体关系
  • 直接检索:基于主键的特定实体获取

RAG 工作流集成

系统包含专门的 RAG 检索指导,定义了动态上下文检索的步骤: 7

  1. 查询分析:识别关键实体、概念和所需信息类型
  2. 优先检索策略:选择最合适的 ConPort 工具进行搜索
  3. 上下文扩展:使用链接项目获取相关上下文
  4. 合成过滤:构建简洁、高度相关的上下文集

向量存储实现

ConPort 使用 ChromaDB 作为向量存储后端: 8

向量数据存储在每个工作空间的 context_portal/conport_vector_data/ 目录中,与 SQLite 数据库并行管理。 9

Notes

ConPort 不仅仅是一个通用的 RAG 平台,而是专门为软件开发项目设计的结构化知识管理系统。它通过 MCP (Model Context Protocol) 协议与 AI 助手集成,提供了约 30 个专门的工具来管理项目上下文、决策记录、进度跟踪等开发相关的知识实体。

Wiki pages you might want to explore:

Citations

File: README.md (L36-36)

ConPort provides a robust and structured way for AI assistants to store, retrieve, and manage various types of project context. It effectively builds a **project-specific knowledge graph**, capturing entities like decisions, progress, and architecture, along with their relationships. This structured knowledge base, enhanced by **vector embeddings** for semantic search, then serves as a powerful backend for **Retrieval Augmented Generation (RAG)**, enabling AI assistants to access precise, up-to-date information for more context-aware and accurate responses.

File: README.md (L47-47)

- Enables building a dynamic **project knowledge graph** with explicit relationships between context items.

File: README.md (L48-48)

- Includes **vector data storage** and **semantic search** capabilities to power advanced RAG.

File: README.md (L49-49)

- Serves as an ideal backend for **Retrieval Augmented Generation (RAG)**, providing AI with precise, queryable project memory.

File: conport_mcp_deep_dive.md (L23-24)

*   **Vector Embeddings & Semantic Search:** ConPort generates and stores vector embeddings for key text content within various entities, enabling semantic similarity search.
*   **RAG Enablement:** The system is designed to be a core component for Retrieval Augmented Generation (RAG) workflows. Its rich querying capabilities (FTS, semantic search, direct retrieval, graph traversal) allow AI agents to fetch relevant context to augment their generative tasks, leading to more accurate and grounded outputs.

File: src/context_portal_mcp/handlers/mcp_handlers.py (L644-648)

async def handle_semantic_search_conport(args: models.SemanticSearchConportArgs) -> List[Dict[str, Any]]:
    """
    Handles the 'semantic_search_conport' MCP tool.
    Performs a semantic search using embeddings and vector store, with optional metadata filters.
    """

File: conport-custom-instructions/roo_code_conport_strategy (L305-310)

  dynamic_context_retrieval_for_rag:
    description: |
      Guidance for dynamically retrieving and assembling context from ConPort to answer user queries or perform tasks,
      enhancing Retrieval Augmented Generation (RAG) capabilities.
    trigger: "When the AI needs to answer a specific question, perform a task requiring detailed project knowledge, or generate content based on ConPort data."
    goal: "To construct a concise, highly relevant context set for the LLM, improving the accuracy and relevance of its responses."

File: src/context_portal_mcp/db/vector_store_service.py (L52-55)

    # Path for ChromaDB persistence.
    # It will be located at: [workspace_id]/context_portal/conport_vector_data/
    # This aligns with the SQLite DB being in [workspace_id]/context_portal/context.db
    # workspace_id is assumed to be the root path of the user's project.

File: src/context_portal_mcp/db/vector_store_service.py (L62-62)

    vector_db_path = os.path.join(context_portal_base_dir, "conport_vector_data") # No leading dot, and inside context_portal

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions