opensrc-mcp
用于获取和查询依赖源代码的代码模式MCP服务器。
为什么?
传统的MCP将工具直接暴露给LLM。此服务器使用 编码模式:代理编写执行服务器端的JavaScript,只返回结果。优点:
- 上下文高效 -大型源代码树保持服务器端
- 批量操作 -一次调用搜索/读取多个文件
- LLM更擅长代码 -JS的训练数据比工具调用更多
安装
npm install -g opensrc-mcp
# or
npx opensrc-mcpOpenCode配置
添加到您的OpenCode配置(~/.config/opencode/config.json 或项目 opencode.json):
{
"mcp": {
"opensrc": {
"type": "local",
"command": "npx",
"args": ["-y", "opensrc-mcp"]
}
}
}工具
execute
单个工具显示所有操作。代理编写运行服务器端的JS;只返回结果。
// Available in sandbox:
declare const opensrc: {
// Read operations
list(): Source[];
has(name: string, version?: string): boolean;
get(name: string): Source | undefined;
files(sourceName: string, glob?: string): Promise;
tree(sourceName: string, options?: { depth?: number }): Promise;
grep(pattern: string, options?: {
sources?: string[];
include?: string;
maxResults?: number;
}): Promise;
astGrep(sourceName: string, pattern: string, options?: {
glob?: string;
lang?: string | string[];
limit?: number;
}): Promise;
read(sourceName: string, filePath: string): Promise;
readMany(sourceName: string, paths: string[]): Promise>;
resolve(spec: string): Promise
;
// Mutation operations
fetch(specs: string | string[], options?: { modify?: boolean }): Promise;
remove(names: string[]): Promise;
clean(options?: {
packages?: boolean;
repos?: boolean;
npm?: boolean;
pypi?: boolean;
crates?: boolean;
}): Promise;
};
declare const sources: Source[]; // All fetched sources
declare const cwd: string; // Project directory示例:
// List all fetched sources
async () => opensrc.list()
// Fetch npm package (auto-detects version from lockfile)
async () => opensrc.fetch("zod")
// Fetch multiple packages
async () => opensrc.fetch(["zod", "drizzle-orm", "hono"])
// Fetch GitHub repo at specific ref
async () => opensrc.fetch("vercel/ai@v3.0.0")
// Fetch from other registries
async () => opensrc.fetch("pypi:requests")
async () => opensrc.fetch("crates:serde")
// Get directory tree
async () => opensrc.tree("zod", { depth: 2 })
// Find TypeScript files
async () => opensrc.files("zod", "**/*.ts")
// Text search
async () => opensrc.grep("parse", { sources: ["zod"], include: "*.ts" })
// AST search (structural pattern matching)
async () => opensrc.astGrep("zod", "function $NAME($$$ARGS)", { glob: "**/*.ts" })
// Read a specific file
async () => opensrc.read("zod", "src/index.ts")
// Read multiple files (supports globs)
async () => opensrc.readMany("zod", ["src/index.ts", "packages/*/package.json"])
// Remove a source
async () => opensrc.remove(["zod"])
// Clean all npm packages
async () => opensrc.clean({ npm: true })包格式
| 格式 | 示例 | 说明 |
|---|---|---|
| `` | zod | npm(自动检测版本) |
@ | zod@3.22.0 | npm特定版本 |
npm: | npm:react | 显式npm |
pypi: | pypi:requests | Python/PyPI |
pip: | pip:flask | pypi的别名 |
crates: | crates:serde | 锈迹/裂纹 |
cargo: | cargo:tokio | 板条箱别名 |
owner/repo | vercel/ai | GitHub仓库 |
owner/repo@ref | vercel/ai@v1.0.0 | GitHub at ref |
github:owner/repo | github:facebook/react | 明确的GitHub |
存储
来源存储在全球 ~/.local/share/opensrc/ (XDG兼容):
~/.local/share/opensrc/
├── sources.json # Index of fetched sources
├── packages/ # npm/pypi/crates packages
│ └── zod/
│ ├── src/
│ ├── package.json
│ └── ...
└── repos/ # GitHub repos
└── github.com/
└── vercel/
└── ai/覆盖 $OPENSRC_DIR 或 $XDG_DATA_HOME.
运作原理
- 客服电话
executeJS代码工具:async () => opensrc.fetch("zod") - 代码在沙盒中运行
vm注入上下文opensrcAPI - 服务器通过以下方式获取包 opensrc (处理注册表查找、git克隆)
- 只有结果返回到代理上下文
┌─────────────────────────────────────────────────────────────┐
│ Agent Context │
├─────────────────────────────────────────────────────────────┤
│ Tool call: execute({ code: "async () => opensrc.fetch..." })│
│ ↓ │
│ Result: { success: true, source: { name: "zod", ... } } │
└─────────────────────────────────────────────────────────────┘
↕
┌─────────────────────────────────────────────────────────────┐
│ opensrc-mcp Server │
├─────────────────────────────────────────────────────────────┤
│ Sandbox executes code with injected opensrc API │
│ Full source tree stays here, never sent to agent │
└─────────────────────────────────────────────────────────────┘许可证
麻省理工学院
