codn_ts
Version:
智能代码分析工具 - 支持语义搜索、调用链分析和代码结构可视化,对大模型/AI agent 友好
159 lines • 6.52 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.startMcpServer = startMcpServer;
const search_1 = require("./search");
const index_1 = require("./index");
const show_1 = require("./show");
const http = __importStar(require("http"));
// MCP 能力声明
const mcpDescribe = {
resources: [
{
id: "codn_files",
type: "file",
description: "项目文件资源"
}
],
tools: [
{
id: "codn_search",
name: "代码搜索",
description: "智能搜索项目中的文件、符号、内容",
parameters: { query: "string", type: "string", limit: "number" }
},
{
id: "codn_analyze",
name: "引用分析",
description: "分析函数/方法引用关系",
parameters: { project: "string" }
},
{
id: "codn_ref",
name: "符号引用分析",
description: "分析特定符号的引用关系,显示调用方和被调用方",
parameters: {
project: "string",
symbol: "string",
includeExternal: "boolean",
maxDepth: "number",
caseSensitive: "boolean"
}
},
{
id: "codn_show",
name: "代码展示",
description: "展示文件区间或类/函数定义",
parameters: { file: "string", symbol: "string", mode: "string" }
}
]
};
function startMcpServer(port = 7788) {
const server = http.createServer(async (req, res) => {
// MCP 能力声明
if (req.method === "GET" && req.url === "/mcp/describe") {
res.writeHead(200, { "Content-Type": "application/json" });
res.end(JSON.stringify(mcpDescribe));
return;
}
// MCP JSON-RPC 2.0 工具调用
if (req.method === "POST" && req.url === "/mcp/rpc") {
let body = "";
req.on("data", chunk => (body += chunk));
req.on("end", async () => {
try {
const rpc = JSON.parse(body);
let result;
if (rpc.method === "codn_search") {
result = await (0, search_1.searchProject)(rpc.params.query, ".", rpc.params);
}
else if (rpc.method === "codn_analyze") {
result = await (0, index_1.analyzeProjectReferences)(rpc.params.project);
}
else if (rpc.method === "codn_ref") {
result = await (0, index_1.analyzeSymbolReferences)(rpc.params.project, rpc.params.symbol, {
includeExternal: rpc.params.includeExternal,
maxDepth: rpc.params.maxDepth,
caseSensitive: rpc.params.caseSensitive
});
}
else if (rpc.method === "codn_show") {
if (rpc.params.symbol) {
result = await (0, show_1.showClassDefinition)(rpc.params);
}
else {
result = (0, show_1.showFileRange)(rpc.params);
}
}
else {
throw new Error("Unknown method");
}
res.writeHead(200, { "Content-Type": "application/json" });
res.end(JSON.stringify({ jsonrpc: "2.0", id: rpc.id, result }));
}
catch (err) {
res.writeHead(200, { "Content-Type": "application/json" });
res.end(JSON.stringify({ jsonrpc: "2.0", id: (JSON.parse(body).id || null), error: { message: err.message } }));
}
});
return;
}
// 兼容原有 /help 路径
if (req.method === "GET" && (req.url === "/" || req.url === "/help")) {
res.writeHead(200, { "Content-Type": "application/json" });
res.end(JSON.stringify({
service: "codn_ts MCP API (MCP 兼容模式)",
mcpDescribePath: "/mcp/describe",
mcpRpcPath: "/mcp/rpc",
doc: "本服务已兼容 MCP 规范,可被 Claude Desktop/Agent 生态自动发现和调用。推荐使用 MCP Inspector 或 Claude Desktop 进行集成。",
exampleRpc: {
jsonrpc: "2.0",
id: "1",
method: "codn_search",
params: { query: "main", type: "all", limit: 5 }
}
}, null, 2));
return;
}
// 其它路径 404
res.writeHead(404, { "Content-Type": "application/json" });
res.end(JSON.stringify({ error: "Not found" }));
});
server.listen(port, () => {
console.log(`MCP server (MCP 兼容模式) listening on port ${port}`);
});
return server;
}
//# sourceMappingURL=mcp.js.map