Elffuss BraidHub

kikocis/continue public

1cfd9a0cd983 · 3.2 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: "Model Context Protocol",
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: "Model Context Protocol",
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 return uri.replace(`{${TEMPLATE_VAR}}`, encodeURIComponent(query));
60 }
61 return uri;
62 }
63
64 async getContextItems(
65 query: string,
66 extras: ContextProviderExtras,
67 ): Promise<ContextItem[]> {
68 const { mcpId, uri } = MCPContextProvider.decodeMCPResourceId(query);
69
70 const connection = MCPManagerSingleton.getInstance().getConnection(mcpId);
71 if (!connection) {
72 throw new Error(`No MCP connection found for ${mcpId}`);
73 }
74
75 const { contents } = await connection.client.readResource({
76 uri: this.insertInputToUriTemplate(uri, extras.fullInput),
77 });
78
79 return await Promise.all(
80 contents.map(async (resource) => {
81 const content = resource.text;
82 if (typeof content !== "string") {
83 throw new Error(
84 "Continue currently only supports text resources from MCP",
85 );
86 }
87
88 return {
89 name: resource.uri,
90 description: resource.uri,
91 content,
92 uri: {
93 type: "url",
94 value: resource.uri,
95 },
96 };
97 }),
98 );
99 }
100
101 async loadSubmenuItems(
102 args: LoadSubmenuItemsArgs,
103 ): Promise<ContextSubmenuItem[]> {
104 return (this.options as MCPContextProviderOptions).submenuItems.map(
105 (item) => ({
106 ...item,
107 id: JSON.stringify({
108 mcpId: (this.options as MCPContextProviderOptions).mcpId,
109 uri: item.id,
110 }),
111 }),
112 );
113 }
114}
115
116export default MCPContextProvider;