@@ -2,11 +2,12 @@ import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
22import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js" ;
33import { z } from "zod" ;
44import { responseSchema , toolParameterSchema , toolSchema } from "./types.js" ;
5- import { toolServices } from "./services/toolServices.js" ;
6-
5+ import { ToolsService } from "./services/toolsService.js" ;
76import { readFileSync } from "fs" ;
87import path from "path" ;
98import { fileURLToPath } from "url" ;
9+ import { RequestOptions } from "https" ;
10+ import { ElicitRequest , ElicitResult } from "@modelcontextprotocol/sdk/types.js" ;
1011
1112const __dirname = path . dirname ( fileURLToPath ( import . meta. url ) ) ;
1213const 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}
0 commit comments