-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Copy pathmcp.ts
46 lines (42 loc) · 1.26 KB
/
mcp.ts
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
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
import { ChatMessage, SlashCommand } from "../../index.js";
import { renderChatMessage } from "../../util/messageContent.js";
export function constructMcpSlashCommand(
client: Client,
name: string,
description?: string,
args?: string[],
): SlashCommand {
return {
name,
description: description ?? "MCP Prompt",
params: {},
run: async function* (context) {
const argsObject: { [key: string]: string } = {};
const userInput = context.input.split(" ").slice(1).join(" ");
if (args) {
args.forEach((arg, i) => {
argsObject[arg] = userInput;
});
}
const result = await client.getPrompt({ name, arguments: argsObject });
const messages: ChatMessage[] = result.messages.map((msg) => {
if (msg.content.type !== "text") {
throw new Error(
"Continue currently only supports text prompts through MCP",
);
}
return {
content: msg.content.text,
role: msg.role,
};
});
for await (const chunk of context.llm.streamChat(
messages,
new AbortController().signal,
)) {
yield renderChatMessage(chunk);
}
},
};
}