check for mcp client attrubutes

This commit is contained in:
Adam Shedivy 2024-12-06 13:01:56 -06:00
parent 886b238e1f
commit 3d41446978
2 changed files with 75 additions and 54 deletions

View file

@ -87,6 +87,15 @@ class MCPConnectionSingleton {
}
}
async checkClientAttribute(func: () => Promise<any>): Promise<any> {
try {
const result = await func();
return result;
} catch (e) {
return null;
}
}
async modifyConfig(config: ContinueConfig): Promise<ContinueConfig> {
try {
await this.connectClient();
@ -96,60 +105,72 @@ class MCPConnectionSingleton {
return config;
}
}
// Resources <—> Context Provider
const { resources } = await this.client.listResources();
const submenuItems = resources.map((resource: any) => ({
title: resource.name,
description: resource.description,
id: resource.uri,
}));
if (!config.contextProviders) {
config.contextProviders = [];
}
config.contextProviders!.push(
new MCPContextProvider({
submenuItems,
client: this.client,
}),
);
// Tools <—> Tools
const { tools } = await this.client.listTools();
const continueTools: Tool[] = tools.map((tool) => ({
displayTitle: tool.name,
function: {
description: tool.description,
name: tool.name,
parameters: tool.inputSchema,
},
readonly: false,
type: "function",
wouldLikeTo: `use the ${tool.name} tool`,
uri: `mcp://${tool.name}`,
}));
config.tools = [...config.tools, ...continueTools];
// Prompts <—> Slash commands
const { prompts } = await this.client.listPrompts();
if (!config.slashCommands) {
config.slashCommands = [];
}
const slashCommands: SlashCommand[] = prompts.map((prompt) => {
return constructMcpSlashCommand(
this.client,
prompt.name,
prompt.description,
prompt.arguments?.map((a) => a.name),
const rawResources = await this.checkClientAttribute(() => this.client.listResources());
if (rawResources && rawResources.resources) {
const submenuItems = rawResources.resources.map((resource: any) => ({
title: resource.name,
description: resource.description,
id: resource.uri,
}));
if (!config.contextProviders) {
config.contextProviders = [];
}
config.contextProviders.push(
new MCPContextProvider({
submenuItems,
client: this.client,
}),
);
});
config.slashCommands!.push(...slashCommands);
} else {
console.warn("Failed to retrieve resources or resources are empty");
}
// Tools <—> Tools
const rawTools = await this.checkClientAttribute(() => this.client.listTools());
if (rawTools && rawTools.tools) {
const continueTools: Tool[] = rawTools.tools.map((tool: any) => ({
displayTitle: tool.name,
function: {
description: tool.description,
name: tool.name,
parameters: tool.inputSchema,
},
readonly: false,
type: "function",
wouldLikeTo: `use the ${tool.name} tool`,
uri: `mcp://${tool.name}`,
}));
config.tools = [...config.tools, ...continueTools];
} else {
console.warn("Failed to retrieve tools or tools are empty");
}
// Prompts <—> Slash commands
const rawPrompts = await this.checkClientAttribute(() => this.client.listPrompts());
if (rawPrompts && rawPrompts.prompts) {
if (!config.slashCommands) {
config.slashCommands = [];
}
const slashCommands: SlashCommand[] = rawPrompts.prompts.map((prompt: any) =>
constructMcpSlashCommand(
this.client,
prompt.name,
prompt.description,
prompt.arguments?.map((a: any) => a.name),
)
);
config.slashCommands.push(...slashCommands);
} else {
console.warn("Failed to retrieve prompts or prompts are empty");
}
return config;
}
}

View file

@ -1,12 +1,12 @@
{
"name": "continue",
"version": "0.9.244",
"version": "0.9.240",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "continue",
"version": "0.9.244",
"version": "0.9.240",
"license": "Apache-2.0",
"dependencies": {
"@electron/rebuild": "^3.2.10",