码智
SourceSage 是一个 MCP(模型上下文协议)服务器,能够高效记忆代码库的关键方面——逻辑、风格和标准——同时允许动态更新和快速检索。它被设计为与语言无关,利用 LLM 对多种语言代码的理解。
MCP 服务配置
复制以下 JSON 到 OPClaw 或其他 MCP 客户端的配置文件中即可使用
{
"mcpServers": {
"sourcesage": {
"args": [],
"command": "sourcesage"
}
}
}
服务介绍
SourceSage: 高效的代码记忆库,专为大语言模型设计
SourceSage 是一个 MCP(模型上下文协议)服务器,它能够高效地记住代码库的关键方面——逻辑、风格和标准,同时支持动态更新和快速检索。它的设计目标是与编程语言无关,利用大语言模型对多种编程语言的理解能力。
特性
- 与编程语言无关:适用于大语言模型理解的任何编程语言
- 知识图谱存储:高效存储代码实体、关系、模式和风格规范
- 基于LLM的分析:依靠大语言模型来分析代码并提供见解
- 令牌高效存储:在最大化内存容量的同时优化最小化令牌使用
- 增量更新:当代码发生变化时更新知识,而不会产生冗余存储
- 快速检索:实现相关信息的快速准确检索
工作原理
SourceSage 使用了一种新颖的方法:
- 大语言模型分析代码文件(任何语言)
- 大语言模型使用MCP工具注册实体、关系、模式和风格规范
- SourceSage 将这些知识以令牌高效的图形结构形式存储
- 当需要时,大语言模型可以查询这些知识
这种方法利用了大语言模型固有的语言理解能力,同时让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
- 打开桌面版Claude
- 转到设置 > 开发者 > 编辑配置
- 在您的
claude_desktop_config.json文件中添加以下内容:
如果您已安装包:
{
"mcpServers": {
"sourcesage": {
"command": "sourcesage",
"args": []
}
}
}
如果您从本地目录运行而不安装:
{
"sourcesage": {
"command": "uv",
"args": [
"--directory",
"/path/to/sourcesage",
"run",
"main.py"
]
},
}
- 重启桌面版Claude
可用工具
SourceSage 提供了以下MCP工具:
-
register_entity: Register a code entity in the knowledge graph
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 -
register_relationship: Register a relationship between entities
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 -
register_pattern: Register a code 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 -
register_style_convention: Register a coding 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 -
add_entity_observation: Add an observation to an entity
Input: - entity_name: Name of the entity - observation: Observation to add Output: Confirmation message -
query_entities: Query entities in the knowledge graph
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 -
get_entity_details: Get detailed information about an entity
Input: - entity_name: Name of the entity Output: Detailed information about the entity -
query_patterns: Query code patterns in the knowledge graph
Input: - language: Filter by programming language (optional) - pattern_name: Filter by pattern name (optional) Output: List of matching patterns -
query_style_conventions: Query coding style conventions
Input: - language: Filter by programming language (optional) - convention_name: Filter by convention name (optional) Output: List of matching style conventions -
get_knowledge_statistics: Get statistics about the knowledge graph
Input: None Output: Statistics about the knowledge graph -
clear_knowledge: Clear all knowledge from the graph
Input: None Output: Confirmation message
示例工作流与 Claude
-
分析代码:请 Claude 分析你的代码文件
"请分析这个 Python 文件并注册关键实体和关系。" -
注册实体:Claude 将使用 register_entity 工具来存储代码实体
"我将在这个文件中注册主要的类。" -
注册关系:Claude 将使用 register_relationship 工具来存储关系
"我将注册这些类之间的继承关系。" -
查询知识:之后,可以向 Claude 询问有关你的代码库的信息
"我的代码库中定义了哪些类?" "给我展示 User 类的详细信息。" "User 类和 Profile 类之间有什么关系?" -
获取编码模式:询问 Claude 关于编码模式的信息
"在我的代码库中使用了哪些设计模式?" "给我展示我的代码中工厂模式的例子。"
它的不同之处
与传统的代码分析工具不同,SourceSage:
- 利用 LLM 的理解能力:利用 LLM 跨语言理解代码语义的能力
- 存储语义知识:专注于意义和关系,而不仅仅是语法
- 语言无关:适用于 LLM 理解的任何编程语言
- 优化令牌效率:以减少令牌使用的方式存储知识
- 随着 LLM 功能的发展而进化:随着 LLM 的改进,代码理解也会随之提高
贡献
欢迎贡献!请随时提交 Pull Request。
许可证
本项目采用 MIT 许可证 - 详情请参阅 LICENSE 文件。