-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.ts
More file actions
94 lines (83 loc) · 2.72 KB
/
server.ts
File metadata and controls
94 lines (83 loc) · 2.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import {
McpServer,
ResourceTemplate,
} from "@modelcontextprotocol/sdk/server/mcp.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import { z } from "zod";
import { GitHubClient, GrepAppClient } from "./client";
const server = new McpServer({
name: "code_search",
version: "1.0.0",
});
const grepApp = new GrepAppClient();
const github = new GitHubClient();
// Add an addition tool
server.tool(
"search_on_github",
"Search the actual code used on GitHub.",
{
code: z.string(),
page: z.number(),
limit: z.number(),
},
async ({ code, page, limit }) => {
const result = await grepApp.search(code, limit, page);
let message = `Found ${result.hits.total} results for "${code}":\n\n`;
if (result.hits.hits.length === 0) {
message = `No results found for "${code}".`;
} else {
result.hits.hits.forEach((hit, index) => {
message += `${
index + 1
}. **${hit.repo.raw}/${hit.path.raw}**\n`;
message += `Branch: ${hit.branch.raw}\n`;
const indentedSnippet = hit.content.snippet;
message += `\`\`\`\n${indentedSnippet}\n\`\`\`\n`;
});
if (result.hits.total > result.hits.hits.length) {
message +=
`(Showing ${result.hits.hits.length} of ${result.hits.total} results)\n`;
}
}
return {
content: [{ type: "text", text: message }],
};
},
);
server.tool(
"get_file_from_github",
"Get the file from GitHub.",
{
"repo": z.string(),
"path": z.string(),
"branch": z.string(),
"linestart": z.number(),
"lineend": z.number(),
},
async ({ repo, path, branch, linestart, lineend }) => {
const code = await github.get(repo, path, branch);
let message =
`Found the file ${path} from ${repo} on branch ${branch}:\n\n`;
const maxLines = code.split("\n").length;
const startLine = Math.max(0, linestart - 1);
message += `\`\`\`\n${
code
.split("\n")
.slice(startLine, lineend)
.map((line, index) => {
return `${startLine + index + 1}: ${line}`;
})
.join("\n")
}\n\`\`\`\n`;
if (lineend > maxLines) {
message += `\n\n(Showing ${
lineend - startLine
} of ${maxLines} lines)\n`;
}
return {
content: [{ type: "text", text: message }],
};
},
);
const transport = new StdioServerTransport();
await server.connect(transport);