Skip to content

Commit 5f4391b

Browse files
authored
[MCP] Support elicitation in create example tool (#28334)
1 parent d1566db commit 5f4391b

File tree

7 files changed

+283
-229
lines changed

7 files changed

+283
-229
lines changed

tools/Mcp/src/CodegenServer.ts

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
22
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
33
import { z } from "zod";
44
import { responseSchema, toolParameterSchema, toolSchema } from "./types.js";
5-
import { toolServices } from "./services/toolServices.js";
6-
5+
import { ToolsService } from "./services/toolsService.js";
76
import { readFileSync } from "fs";
87
import path from "path";
98
import { fileURLToPath } from "url";
9+
import { RequestOptions } from "https";
10+
import { ElicitRequest, ElicitResult } from "@modelcontextprotocol/sdk/types.js";
1011

1112
const __dirname = path.dirname(fileURLToPath(import.meta.url));
1213
const srcPath = path.resolve(__dirname, "..", "src");
@@ -37,6 +38,31 @@ export class CodegenServer {
3738
this.initPrompts();
3839
}
3940

41+
// dummy method for sending sampling request
42+
public createMessage() {
43+
return this._mcp.server.createMessage({
44+
messages: [
45+
{
46+
role: "user",
47+
content: {
48+
type: "text",
49+
text: `This is a test sampling request`,
50+
},
51+
},
52+
],
53+
maxTokens: 500
54+
});
55+
}
56+
57+
// server elicitation request
58+
public elicitInput(
59+
params: ElicitRequest["params"],
60+
options?: RequestOptions
61+
): Promise<ElicitResult> {
62+
//TODO: add log
63+
return this._mcp.server.elicitInput(params, options);
64+
}
65+
4066
public static getInstance(): CodegenServer {
4167
if (!CodegenServer._instance) {
4268
CodegenServer._instance = new CodegenServer();
@@ -50,10 +76,11 @@ export class CodegenServer {
5076

5177

5278
initTools() {
79+
const toolsService = ToolsService.getInstance().setServer(this);
5380
const toolSchemas = specs.tools as toolSchema[];
5481
for (const schema of toolSchemas) {
55-
const parameter = this.createToolParameterfromSchema(schema.parameters);
56-
const callBack = toolServices<{ [k: string]: z.ZodTypeAny }>(schema.callbackName, this._responses.get(schema.name));
82+
const parameter = toolsService.createToolParameterfromSchema(schema.parameters);
83+
const callBack = toolsService.getTools<{ [k: string]: z.ZodTypeAny }>(schema.callbackName, this._responses.get(schema.name));
5784
this._mcp.tool(
5885
schema.name,
5986
schema.description,
@@ -88,31 +115,4 @@ export class CodegenServer {
88115
this._responses.set(response.name, response.text);
89116
});
90117
}
91-
92-
createToolParameterfromSchema(schemas: toolParameterSchema[]){
93-
const parameter: {[k: string]: z.ZodTypeAny} = {};
94-
for (const schema of schemas) {
95-
switch (schema.type) {
96-
case "string":
97-
parameter[schema.name] = z.string().describe(schema.description);
98-
break;
99-
case "number":
100-
parameter[schema.name] = z.number().describe(schema.description);
101-
break;
102-
case "boolean":
103-
parameter[schema.name] = z.boolean().describe(schema.description);
104-
break;
105-
case "array":
106-
parameter[schema.name] = z.array(z.string()).describe(schema.description);
107-
break;
108-
// object parameter not supported yet
109-
// case "object":
110-
// parameter[schema.name] = z.object({}).describe(input.description); // Placeholder for object type
111-
// break;
112-
default:
113-
throw new Error(`Unsupported parameter type: ${schema.type}`);
114-
}
115-
}
116-
return parameter;
117-
}
118118
}
File renamed without changes.

tools/Mcp/src/services/toolServices.ts

Lines changed: 0 additions & 169 deletions
This file was deleted.

0 commit comments

Comments
 (0)