Elffuss BraidHub

kikocis/continue public

main · 3.3 KiBRawPermalink
1import { BaseContextProvider } from "../";
2import {
3 ContextItem,
4 ContextProviderDescription,
5 ContextProviderExtras,
6 ContextSubmenuItem,
7 LoadSubmenuItemsArgs,
8} from "../../";
9import { MCPManagerSingleton } from "../mcp/MCPManagerSingleton";
10
11interface MCPContextProviderOptions {
12 mcpId: string;
13 serverName: string;
14 submenuItems: ContextSubmenuItem[];
15}
16
17class MCPContextProvider extends BaseContextProvider {
18 static description: ContextProviderDescription = {
19 title: "mcp",
20 displayTitle: "MCP",
21 description: "MCP Resources",
22 type: "submenu",
23 renderInlineAs: "",
24 };
25 override get description(): ContextProviderDescription {
26 return {
27 title: `${MCPContextProvider.description.title}-${this.options["mcpId"]}`,
28 displayTitle: this.options["serverName"]
29 ? `${this.options["serverName"]} resources`
30 : "MCP",
31 renderInlineAs: "",
32 description: "MCP Resources",
33 type: "submenu",
34 };
35 }
36
37 static encodeMCPResourceId(mcpId: string, uri: string): string {
38 return JSON.stringify({ mcpId, uri });
39 }
40
41 static decodeMCPResourceId(mcpResourceId: string): {
42 mcpId: string;
43 uri: string;
44 } {
45 return JSON.parse(mcpResourceId);
46 }
47
48 constructor(options: MCPContextProviderOptions) {
49 super(options);
50 }
51
52 /**
53 * Continue experimentally supports resource templates (https://modelcontextprotocol.io/docs/concepts/resources#resource-templates)
54 * by allowing specifically just the "query" variable in the template, which we will update with the full input of the user in the input box
55 */
56 private insertInputToUriTemplate(uri: string, query: string): string {
57 const TEMPLATE_VAR = "query";
58 if (uri.includes(`{${TEMPLATE_VAR}}`)) {
59 // Sending an empty string will result in an error, so we instead send "null"
60 const queryOrNull = query.trim() === "" ? "null" : query;
61 return uri.replace(`{${TEMPLATE_VAR}}`, encodeURIComponent(queryOrNull));
62 }
63 return uri;
64 }
65
66 async getContextItems(
67 query: string,
68 extras: ContextProviderExtras,
69 ): Promise<ContextItem[]> {
70 const { mcpId, uri } = MCPContextProvider.decodeMCPResourceId(query);
71
72 const connection = MCPManagerSingleton.getInstance().getConnection(mcpId);
73 if (!connection) {
74 throw new Error(`No MCP connection found for ${mcpId}`);
75 }
76
77 const { contents } = await connection.client.readResource({
78 uri: this.insertInputToUriTemplate(uri, extras.fullInput),
79 });
80
81 return await Promise.all(
82 contents.map(async (resource) => {
83 const content = resource.text;
84 if (typeof content !== "string") {
85 throw new Error(
86 "Continue currently only supports text resources from MCP",
87 );
88 }
89
90 return {
91 name: resource.uri,
92 description: resource.uri,
93 content,
94 uri: {
95 type: "url",
96 value: resource.uri,
97 },
98 };
99 }),
100 );
101 }
102
103 async loadSubmenuItems(
104 args: LoadSubmenuItemsArgs,
105 ): Promise<ContextSubmenuItem[]> {
106 return (this.options as MCPContextProviderOptions).submenuItems.map(
107 (item) => ({
108 ...item,
109 id: JSON.stringify({
110 mcpId: (this.options as MCPContextProviderOptions).mcpId,
111 uri: item.id,
112 }),
113 }),
114 );
115 }
116}
117
118export default MCPContextProvider;