Skip to main content
Glama

SourceSage:LLM 的高效代码内存

SourceSage 是一款 MCP(模型上下文协议)服务器,能够高效地记忆代码库的关键要素——逻辑、代码风格和标准,同时支持动态更新和快速检索。它的设计与语言无关,充分利用了法学硕士 (LLM) 对多种语言代码的理解能力。

特征

  • 语言无关:适用于 LLM 理解的任何编程语言

  • 知识图谱存储:高效存储代码实体、关系、模式和样式约定

  • LLM 驱动分析:依靠 LLM 分析代码并提供见解

  • 令牌高效存储:优化最小令牌使用量,同时最大化内存容量

  • 增量更新:代码更改时更新知识,无需冗余存储

  • 快速检索:能够快速准确地检索相关信息

Related MCP server: Logseq MCP Tools

工作原理

SourceSage 采用了一种新颖的方法:

  1. LLM 分析代码文件(任何语言)

  2. LLM 使用 MCP 工具来注册实体、关系、模式和样式约定

  3. SourceSage 将这些知识存储在令牌高效的图形结构中

  4. LLM 可以在以后需要时查询这些知识

这种方法利用了 LLM 固有的语言理解能力,同时将 MCP 服务器的重点放在高效的内存管理上。

安装

# Clone the repository
git clone https://github.com/yourusername/sourcesage.git
cd sourcesage

# Install the package
pip install -e .

用法

运行 MCP 服务器

# Run the server
sourcesage

# Or run directly from the repository
python -m sourcesage.mcp_server

连接到 Claude 桌面版

  1. 打开 Claude 桌面版

  2. 前往“设置”>“开发者”>“编辑配置”

  3. 将以下内容添加到您的claude_desktop_config.json中:

如果您已经安装了该软件包:

{
  "mcpServers": {
    "sourcesage": {
      "command": "sourcesage",
      "args": []
    }
  }
}

如果您从本地目录运行而未安装:

{
  "sourcesage": {
      "command": "uv", 
      "args": [
        "--directory",
        "/path/to/sourcesage",
        "run",
        "main.py"
      ]
    },
}
  1. 重启 Claude 桌面版

可用工具

SourceSage 提供以下 MCP 工具:

  1. register_entity :在知识图谱中注册代码实体

    Input:
      - name: Name of the entity (e.g., class name, function name)
      - entity_type: Type of entity (class, function, module, etc.)
      - summary: Brief description of the entity
      - signature: Entity signature (optional)
      - language: Programming language (optional)
      - observations: List of observations about the entity (optional)
      - metadata: Additional metadata (optional)
    Output: Confirmation message with entity ID
  2. register_relationship :注册实体之间的关系

    Input:
      - from_entity: Name of the source entity
      - to_entity: Name of the target entity
      - relationship_type: Type of relationship (calls, inherits, imports, etc.)
      - metadata: Additional metadata (optional)
    Output: Confirmation message with relationship ID
  3. register_pattern :注册代码模式

    Input:
      - name: Name of the pattern
      - description: Description of the pattern
      - language: Programming language (optional)
      - example: Example code demonstrating the pattern (optional)
      - metadata: Additional metadata (optional)
    Output: Confirmation message with pattern ID
  4. register_style_convention :注册编码风格约定

    Input:
      - name: Name of the convention
      - description: Description of the convention
      - language: Programming language (optional)
      - examples: Example code snippets demonstrating the convention (optional)
      - metadata: Additional metadata (optional)
    Output: Confirmation message with convention ID
  5. add_entity_observation :向实体添加观察

    Input:
      - entity_name: Name of the entity
      - observation: Observation to add
    Output: Confirmation message
  6. query_entities :查询知识图谱中的实体

    Input:
      - entity_type: Filter by entity type (optional)
      - language: Filter by programming language (optional)
      - name_pattern: Filter by name pattern (regex, optional)
      - limit: Maximum number of results to return (optional)
    Output: List of matching entities
  7. get_entity_details :获取实体的详细信息

    Input:
      - entity_name: Name of the entity
    Output: Detailed information about the entity
  8. query_patterns :知识图谱中的查询代码模式

    Input:
      - language: Filter by programming language (optional)
      - pattern_name: Filter by pattern name (optional)
    Output: List of matching patterns
  9. query_style_conventions :查询编码风格约定

    Input:
      - language: Filter by programming language (optional)
      - convention_name: Filter by convention name (optional)
    Output: List of matching style conventions
  10. get_knowledge_statistics :获取有关知识图谱的统计数据

Input: None
Output: Statistics about the knowledge graph
  1. clear_knowledge :清除图中的所有知识

Input: None
Output: Confirmation message

Claude 的示例工作流程

  1. 分析代码:让 Claude 分析你的代码文件

    "Please analyze this Python file and register the key entities and relationships."
  2. 注册实体:Claude 将使用 register_entity 工具来存储代码实体

    "I'll register the main class in this file."
  3. 注册关系:Claude 将使用 register_relationship 工具来存储关系

    "I'll register the inheritance relationship between these classes."
  4. 查询知识:稍后,向 Claude 询问你的代码库

    "What classes are defined in my codebase?"
    "Show me the details of the User class."
    "What's the relationship between the User and Profile classes?"
  5. 获取编码模式:向 Claude 询问编码模式

    "What design patterns are used in my codebase?"
    "Show me examples of the Factory pattern in my code."

有何不同

与传统的代码分析工具不同,SourceSage:

  1. 利用 LLM 理解:利用 LLM 的能力理解跨语言的代码语义

  2. 存储语义知识:关注意义和关系,而不仅仅是语法

  3. 与语言无关:可与 LLM 理解的任何编程语言配合使用

  4. 优化令牌效率:以最小化令牌使用的方式存储知识

  5. 随着 LLM 能力的提升而发展:随着 LLM 的进步,代码理解能力也在提升

贡献

欢迎贡献代码!欢迎提交 Pull 请求。

执照

该项目根据 MIT 许可证获得许可 - 有关详细信息,请参阅 LICENSE 文件。

Available Tools

13 tools
add_entity_observationC

Add an observation to an entity.

Args: entity_name: Name of the entity observation: Observation to add

Inputs: entity_name (required), observation (required).

clear_knowledgeA

Clear all knowledge from the graph.

Inputs: no named parameters.

dump_project_understandingA

Dump understanding of an entire project at once.

This tool provides a comprehensive dump of all knowledge related to a project, including all entities, relationships, patterns, and style conventions.

Args: project_path: Path to the project root directory include_observations: Whether to include detailed observations

Inputs: project_path (required), include_observations.

get_entity_detailsC

Get detailed information about an entity.

Args: entity_name: Name of the entity

Inputs: entity_name (required).

get_knowledge_statisticsC

Get statistics about the knowledge graph.

Inputs: no named parameters.

load_project_understandingB

Load understanding of an entire project at once.

This tool should be used by MCP clients to quickly get project understanding if available, instead of reading all the files individually. It loads all entities, relationships, patterns, and style conventions related to the project.

Args: project_path: Path to the project root directory

Inputs: project_path (required).

query_entitiesB

Query entities in the knowledge graph.

Args: entity_type: Filter by entity type (class, function, module, etc.) language: Filter by programming language name_pattern: Filter by name pattern (regex) limit: Maximum number of results to return

Inputs: entity_type, language, limit, name_pattern.

query_patternsB

Query code patterns in the knowledge graph.

Args: language: Filter by programming language pattern_name: Filter by pattern name

Inputs: language, pattern_name.

query_style_conventionsA

Query coding style conventions in the knowledge graph.

Args: language: Filter by programming language convention_name: Filter by convention name

Inputs: convention_name, language.

register_entityB

Register a code entity in the knowledge graph.

Args: name: Name of the entity (e.g., class name, function name) entity_type: Type of entity (class, function, module, etc.) summary: Brief description of the entity signature: Entity signature (e.g., function signature) language: Programming language observations: List of observations about the entity metadata: Additional metadata as key-value pairs

Inputs: entity_type (required), name (required), summary (required), language, +3 more.

register_patternB

Register a code pattern.

Args: name: Name of the pattern description: Description of the pattern language: Programming language example: Example code demonstrating the pattern metadata: Additional metadata as key-value pairs

Inputs: description (required), name (required), example, language, +1 more.

register_relationshipC

Register a relationship between entities.

Args: from_entity: Name of the source entity to_entity: Name of the target entity relationship_type: Type of relationship (calls, inherits, imports, etc.) metadata: Additional metadata as key-value pairs

Inputs: from_entity (required), relationship_type (required), to_entity (required), metadata.

register_style_conventionC

Register a coding style convention.

Args: name: Name of the convention description: Description of the convention language: Programming language examples: Example code snippets demonstrating the convention metadata: Additional metadata as key-value pairs

Inputs: description (required), name (required), examples, language, +1 more.

Tool Schema Changelog

Recent tool additions, removals, and schema changes observed during successful MCP inspections.

Loading tool schema changelog…

    TDQS

    B3.3/5.0

    Scored across 13 tools

    Disambiguation4/5

    Most tools have distinct purposes focused on different aspects of knowledge graph management (entities, patterns, relationships, style conventions). However, there is some potential overlap between dump_project_understanding and load_project_understanding as both handle project-level data, though their descriptions clarify one is for output and the other for input.

    Naming Consistency5/5

    All tools follow a consistent snake_case naming pattern with clear verb_noun structure (e.g., add_entity_observation, get_entity_details, query_entities, register_entity). The naming is predictable and uniform across all 13 tools.

    Tool Count5/5

    With 13 tools, this is well-scoped for a knowledge graph server covering entities, patterns, relationships, and style conventions. Each tool serves a specific function in the CRUD/lifecycle operations, and none appear redundant or out of place.

    Completeness4/5

    The toolset provides comprehensive coverage for managing a knowledge graph, including registration, querying, and statistics. Minor gaps exist, such as no explicit update or delete operations for entities/patterns/relationships/conventions, but agents can work around this by re-registering or using clear_knowledge.

    Related MCP Connectors

    Related MCP Servers

    • A
      license
      Not graded
      quality
      A
      maintenance
      This is an MCP server for PostgREST. It allows LLMs perform database queries and operations on Postgres databases via PostgREST. This server works with both Supabase projects (which use PostgREST) and standalone PostgREST servers.
      2,128 npm
      3,017
      Apache 2.0
    • A
      license
      Not graded
      quality
      D
      maintenance
      A Model Context Protocol server that provides AI assistants with structured access to your Logseq knowledge graph, enabling retrieval, searching, analysis, and creation of content within your personal knowledge base.
      70
      MIT
    • A
      license
      A
      quality
      D
      maintenance
      An MCP server that connects to a Swagger specification and helps an AI to build all the required models to generate a MCP server for that service.
      5
      167
      MIT