Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support MCP Resource Templates / Dynamic Resources #4617

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions core/context/mcp/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { ContinueConfig, MCPOptions, SlashCommand, Tool } from "../..";
import { constructMcpSlashCommand } from "../../commands/slash/mcp";
import { encodeMCPToolUri } from "../../tools/callTool";
import MCPContextProvider from "../providers/MCPContextProvider";
import { createMCPTemplateContextProviderClass } from "../providers/MCPTemplateContextProvider";

export class MCPManagerSingleton {
private static instance: MCPManagerSingleton;
Expand Down Expand Up @@ -167,6 +168,42 @@ class MCPConnection {
mcpId,
}),
);

const templates = await this.client.listResourceTemplates({}, { signal });
const templateContextProviders = templates.resourceTemplates.map(
(resource: any) => {
const matches = resource.uriTemplate.matchAll(
/{([^}]+)}/g,
) as IterableIterator<RegExpMatchArray>;
const params = Array.from(matches, (m) => m[1]);
if (params.length === 0) {
throw new Error(
`No parameters found in resource template ${resource.name}`,
);
}

if (params.length > 1) {
throw new Error(
`Cant use resource template ${resource.name} with more than one parameter. This is a limitation of the current implementation.`,
);
}

const parameter = params[0];
/// create temporary context provider for each mcp resource template
/// For now this is necessary as we dont have an option to get from a submenu to a query input
const Provider = createMCPTemplateContextProviderClass({
// combine the MCP server name and resource name into a unique display name
name: `${mcpId}.${resource.name}`,
description: resource.description,
});
return new Provider({
mcpId,
parameter,
uri: resource.uriTemplate,
});
},
);
config.contextProviders.push(...templateContextProviders);
}

// Tools <—> Tools
Expand Down
74 changes: 74 additions & 0 deletions core/context/providers/MCPTemplateContextProvider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { ContextItem, ContextProviderExtras } from "../../index.js";
import { BaseContextProvider } from "../index.js";
import { MCPManagerSingleton } from "../mcp";

interface MCPTemplateContextProviderOptions {
name: string;
description: string;
}

class MCPTemplateContextProvider extends BaseContextProvider {
private readonly _mcpId: string;
private readonly _parameter: string;
private readonly _uri: string;

constructor(options: { mcpId: string; parameter: string; uri: string }) {
super(options);
this._mcpId = options.mcpId;
this._parameter = options.parameter;
this._uri = options.uri;
}

async getContextItems(
query: string,
extras: ContextProviderExtras,
): Promise<ContextItem[]> {
const connection = MCPManagerSingleton.getInstance().getConnection(
this._mcpId,
);
if (!connection) {
throw new Error(`No MCP connection found for ${this._mcpId}`);
}

const uri = this._uri.replace(`{${this._parameter}}`, query);

const { contents } = await connection.client.readResource({ uri });

return await Promise.all(
contents.map(async (resource) => {
const content = resource.text;
if (typeof content !== "string") {
throw new Error(
"Continue currently only supports text resources from MCP",
);
}

return {
name: resource.uri,
description: resource.uri,
content,
uri: {
type: "url",
value: resource.uri,
},
};
}),
);
}
}

// Factory to create subclasses with custom static descriptions
export function createMCPTemplateContextProviderClass(
options: MCPTemplateContextProviderOptions,
): typeof MCPTemplateContextProvider {
return class extends MCPTemplateContextProvider {
static description = {
title: options.name,
displayTitle: options.name,
description: options.description,
type: "query",
};
};
}

export default MCPTemplateContextProvider;
Loading