From 7058eacf8465347343fe2fdd0f15c07e3740b338 Mon Sep 17 00:00:00 2001 From: Yash Date: Thu, 26 Jun 2025 00:24:25 +1000 Subject: [PATCH 01/11] Updated tools schema, fixed json reading --- tools/Mcp/package.json | 3 ++- tools/Mcp/src/CodegenServer.ts | 22 +++++++++++++++------- tools/Mcp/src/types.ts | 2 +- 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/tools/Mcp/package.json b/tools/Mcp/package.json index 01a3027772ea..fff41806944a 100644 --- a/tools/Mcp/package.json +++ b/tools/Mcp/package.json @@ -8,7 +8,8 @@ }, "scripts": { "build": "tsc", - "start": "node build/index.js" + "start": "node build/index.js", + "fresh": "npm run build && npm run start" }, "files": [ "build" diff --git a/tools/Mcp/src/CodegenServer.ts b/tools/Mcp/src/CodegenServer.ts index 47a33d0886bb..0b2798be2ab9 100644 --- a/tools/Mcp/src/CodegenServer.ts +++ b/tools/Mcp/src/CodegenServer.ts @@ -2,10 +2,18 @@ import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js"; import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js"; import { z } from "zod"; import { toolParameterSchema, toolSchema } from "./types.js"; -import specs from "./specs/Specs.json" assert { type: "json" }; -import responses from "./specs/responses.json" assert { type: "json" }; import { toolServices } from "./services/toolServices.js"; +import { readFileSync } from "fs"; +import path from "path"; +import { fileURLToPath } from "url"; + +const __dirname = path.dirname(fileURLToPath(import.meta.url)); +const srcPath = path.resolve(__dirname, "..", "src"); + +const specs = JSON.parse(readFileSync(path.join(srcPath, "specs/Specs.json"), "utf-8")); +const responses = JSON.parse(readFileSync(path.join(srcPath, "specs/responses.json"), "utf-8")); + export class CodegenServer { private static _instance: CodegenServer; private _mcp: McpServer; @@ -76,7 +84,7 @@ export class CodegenServer { } initResponses() { - responses?.forEach(response => { + (responses as toolParameterSchema[])?.forEach((response: toolParameterSchema) => { this._responses.set(response.name, response.text); }); } @@ -86,16 +94,16 @@ export class CodegenServer { for (const schema of schemas) { switch (schema.type) { case "string": - parameter[schema.name] = z.string().describe(schema.description); + parameter[schema.name] = z.string().describe(schema.text); break; case "number": - parameter[schema.name] = z.number().describe(schema.description); + parameter[schema.name] = z.number().describe(schema.text); break; case "boolean": - parameter[schema.name] = z.boolean().describe(schema.description); + parameter[schema.name] = z.boolean().describe(schema.text); break; case "array": - parameter[schema.name] = z.array(z.string()).describe(schema.description); + parameter[schema.name] = z.array(z.string()).describe(schema.text); break; // object parameter not supported yet // case "object": diff --git a/tools/Mcp/src/types.ts b/tools/Mcp/src/types.ts index fd5206b5e19f..e5de0fffcaca 100644 --- a/tools/Mcp/src/types.ts +++ b/tools/Mcp/src/types.ts @@ -1,7 +1,7 @@ export type toolParameterSchema = { name: string; type: string; - description: string; + text: string; } export type toolSchema = { From 7e30f9a390c1b43dabdf8ae3cb519324634266c7 Mon Sep 17 00:00:00 2001 From: Yash <55773468+notyashhh@users.noreply.github.com> Date: Thu, 26 Jun 2025 00:48:46 +1000 Subject: [PATCH 02/11] Corrected download url spelling Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- tools/Mcp/src/services/utils.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/Mcp/src/services/utils.ts b/tools/Mcp/src/services/utils.ts index 11642322d44d..ad4d65cd5f12 100644 --- a/tools/Mcp/src/services/utils.ts +++ b/tools/Mcp/src/services/utils.ts @@ -124,7 +124,7 @@ export async function getExamplesFromSpecs(workingDirectory: string): Promise Date: Thu, 26 Jun 2025 00:49:36 +1000 Subject: [PATCH 03/11] Updated import statement to reflect filename Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- tools/Mcp/src/CodegenServer.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/Mcp/src/CodegenServer.ts b/tools/Mcp/src/CodegenServer.ts index 0b2798be2ab9..a648c4c6bfd7 100644 --- a/tools/Mcp/src/CodegenServer.ts +++ b/tools/Mcp/src/CodegenServer.ts @@ -11,7 +11,7 @@ import { fileURLToPath } from "url"; const __dirname = path.dirname(fileURLToPath(import.meta.url)); const srcPath = path.resolve(__dirname, "..", "src"); -const specs = JSON.parse(readFileSync(path.join(srcPath, "specs/Specs.json"), "utf-8")); +const specs = JSON.parse(readFileSync(path.join(srcPath, "specs/specs.json"), "utf-8")); const responses = JSON.parse(readFileSync(path.join(srcPath, "specs/responses.json"), "utf-8")); export class CodegenServer { From 11535772c014680638ceef1bf0ad6192b39c287b Mon Sep 17 00:00:00 2001 From: Yash Date: Thu, 26 Jun 2025 00:51:23 +1000 Subject: [PATCH 04/11] Added usage docs to readme --- tools/Mcp/README.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/tools/Mcp/README.md b/tools/Mcp/README.md index e99d152dabae..204d465f5adc 100644 --- a/tools/Mcp/README.md +++ b/tools/Mcp/README.md @@ -52,6 +52,23 @@ This MCP server is designed to work with Azure PowerShell module development wor ## Usage +### Add as mcp server in Github Copilot Agent mode + +Simple add this mcp server to your user `settings.json` in VsCode to include: + +```json + "mcp": { + "servers": { + "az-pwsh-mcp-server": { + "type": "stdio", + "command": "sh", + "args": ["-c", "npm install && npm run fresh"], + "cwd": "./azure-powershell/tools/Mcp", + }, + } + } +``` + ### As an MCP Server The server runs on stdio and can be integrated with MCP-compatible clients: From fa1392bba2a9fb3c633f14e1df2643dfb3ce8954 Mon Sep 17 00:00:00 2001 From: Yash Date: Thu, 26 Jun 2025 16:29:21 +1000 Subject: [PATCH 05/11] Reverted ToolParameter Schema --- tools/Mcp/src/CodegenServer.ts | 10 +++++----- tools/Mcp/src/types.ts | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/tools/Mcp/src/CodegenServer.ts b/tools/Mcp/src/CodegenServer.ts index a648c4c6bfd7..78c3d233d4f5 100644 --- a/tools/Mcp/src/CodegenServer.ts +++ b/tools/Mcp/src/CodegenServer.ts @@ -85,7 +85,7 @@ export class CodegenServer { initResponses() { (responses as toolParameterSchema[])?.forEach((response: toolParameterSchema) => { - this._responses.set(response.name, response.text); + this._responses.set(response.name, response.description); }); } @@ -94,16 +94,16 @@ export class CodegenServer { for (const schema of schemas) { switch (schema.type) { case "string": - parameter[schema.name] = z.string().describe(schema.text); + parameter[schema.name] = z.string().describe(schema.description); break; case "number": - parameter[schema.name] = z.number().describe(schema.text); + parameter[schema.name] = z.number().describe(schema.description); break; case "boolean": - parameter[schema.name] = z.boolean().describe(schema.text); + parameter[schema.name] = z.boolean().describe(schema.description); break; case "array": - parameter[schema.name] = z.array(z.string()).describe(schema.text); + parameter[schema.name] = z.array(z.string()).describe(schema.description); break; // object parameter not supported yet // case "object": diff --git a/tools/Mcp/src/types.ts b/tools/Mcp/src/types.ts index e5de0fffcaca..9c99b8f1dd3e 100644 --- a/tools/Mcp/src/types.ts +++ b/tools/Mcp/src/types.ts @@ -1,7 +1,7 @@ export type toolParameterSchema = { name: string; + description: string; type: string; - text: string; } export type toolSchema = { From 0d2be2a17f77892843b709e7e1d8c8490717b156 Mon Sep 17 00:00:00 2001 From: Yash Date: Thu, 26 Jun 2025 16:47:37 +1000 Subject: [PATCH 06/11] Added ResponseSchema --- tools/Mcp/src/CodegenServer.ts | 6 +++--- tools/Mcp/src/types.ts | 6 ++++++ 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/tools/Mcp/src/CodegenServer.ts b/tools/Mcp/src/CodegenServer.ts index 78c3d233d4f5..e34173faea28 100644 --- a/tools/Mcp/src/CodegenServer.ts +++ b/tools/Mcp/src/CodegenServer.ts @@ -1,7 +1,7 @@ import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js"; import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js"; import { z } from "zod"; -import { toolParameterSchema, toolSchema } from "./types.js"; +import { responseSchema, toolParameterSchema, toolSchema } from "./types.js"; import { toolServices } from "./services/toolServices.js"; import { readFileSync } from "fs"; @@ -84,8 +84,8 @@ export class CodegenServer { } initResponses() { - (responses as toolParameterSchema[])?.forEach((response: toolParameterSchema) => { - this._responses.set(response.name, response.description); + (responses as responseSchema[])?.forEach((response: responseSchema) => { + this._responses.set(response.name, response.text); }); } diff --git a/tools/Mcp/src/types.ts b/tools/Mcp/src/types.ts index 9c99b8f1dd3e..f4fea8f07c3c 100644 --- a/tools/Mcp/src/types.ts +++ b/tools/Mcp/src/types.ts @@ -11,6 +11,12 @@ export type toolSchema = { callbackName: string; } +export type responseSchema = { + name: string; + type: string; + text: string; +} + export type yamlContent = { [key: string]: any; } From 0b5de6112593651a4c53a8025dc81ad1274b87af Mon Sep 17 00:00:00 2001 From: Yash Date: Fri, 4 Jul 2025 15:06:54 +1000 Subject: [PATCH 07/11] Async -> Sync execution --- tools/Mcp/src/CodegenServer.ts | 102 +++++++++++++++++++------------- tools/Mcp/src/services/utils.ts | 23 +++++-- 2 files changed, 79 insertions(+), 46 deletions(-) diff --git a/tools/Mcp/src/CodegenServer.ts b/tools/Mcp/src/CodegenServer.ts index e34173faea28..f6ef597a18dc 100644 --- a/tools/Mcp/src/CodegenServer.ts +++ b/tools/Mcp/src/CodegenServer.ts @@ -16,11 +16,11 @@ const responses = JSON.parse(readFileSync(path.join(srcPath, "specs/responses.js export class CodegenServer { private static _instance: CodegenServer; - private _mcp: McpServer; + private _server: McpServer; private _responses = new Map(); private constructor() { - this._mcp = new McpServer({ + this._server = new McpServer({ name: "codegen", version: "1.0.0", capabilities: { @@ -32,11 +32,15 @@ export class CodegenServer { } init(): void { - this.initResponses(); + this.readResponses(); this.initTools(); this.initPrompts(); } + /** + * Singleton instance of CodegenServer + * @returns {CodegenServer} The singleton instance + */ public static getInstance(): CodegenServer { if (!CodegenServer._instance) { CodegenServer._instance = new CodegenServer(); @@ -44,51 +48,26 @@ export class CodegenServer { return CodegenServer._instance; } + /** + * Connects the server to a transport layer + * @param {StdioServerTransport} transport - The transport layer to connect to + * @returns {Promise} A promise that resolves when the connection is established + */ public async connect(transport: StdioServerTransport): Promise { - await this._mcp.connect(transport); + await this._server.connect(transport); } - - initTools() { - const toolSchemas = specs.tools as toolSchema[]; - for (const schema of toolSchemas) { - const parameter = this.createToolParameterfromSchema(schema.parameters); - const callBack = toolServices<{ [k: string]: z.ZodTypeAny }>(schema.callbackName, this._responses.get(schema.name)); - this._mcp.tool( - schema.name, - schema.description, - parameter, - (parameter) => callBack(parameter) - ); - } - } - - initPrompts() { - this._mcp.prompt( - "create-greeting", - "Generate a customized greeting message", - { name: z.string().describe("Name of the person to greet"), style: z.string().describe("The style of greeting, such a formal, excited, or casual. If not specified casual will be used")}, - ({ name, style = "casual" }: { name: string, style?: string }) => { - return { - messages: [ - { - role: "user", - content: { - type: "text", - text: `Please generate a greeting in ${style} style to ${name}.`, - }, - }, - ], - }; - }); - } - - initResponses() { + readResponses() { (responses as responseSchema[])?.forEach((response: responseSchema) => { this._responses.set(response.name, response.text); }); } + /** + * Creates tool parameters from the provided schema + * @param {toolParameterSchema[]} schemas - The schemas to create parameters from + * @returns {Object} An object containing the created parameters + */ createToolParameterfromSchema(schemas: toolParameterSchema[]){ const parameter: {[k: string]: z.ZodTypeAny} = {}; for (const schema of schemas) { @@ -115,4 +94,47 @@ export class CodegenServer { } return parameter; } + + /** + * Initializes the prompts for the server + * This method registers a prompt for generating a customized greeting message. + */ + initPrompts() { + this._server.prompt( + "create-greeting", + "Generate a customized greeting message", + { name: z.string().describe("Name of the person to greet"), style: z.string().describe("The style of greeting, such a formal, excited, or casual. If not specified casual will be used")}, + ({ name, style = "casual" }: { name: string, style?: string }) => { + return { + messages: [ + { + role: "user", + content: { + type: "text", + text: `Please generate a greeting in ${style} style to ${name}.`, + }, + }, + ], + }; + }); + } + + /** + * Initializes the tools for the server + * This method registers tools based on the provided specifications. + */ + initTools() { + const toolSchemas = specs.tools as toolSchema[]; + for (const schema of toolSchemas) { + const parameter = this.createToolParameterfromSchema(schema.parameters); + const callBack = toolServices<{ [k: string]: z.ZodTypeAny }>(schema.callbackName, this._responses.get(schema.name)); + this._server.tool( + schema.name, + schema.description, + parameter, + (parameter) => callBack(parameter) + ); + } + } + } \ No newline at end of file diff --git a/tools/Mcp/src/services/utils.ts b/tools/Mcp/src/services/utils.ts index ad4d65cd5f12..a594e3746c87 100644 --- a/tools/Mcp/src/services/utils.ts +++ b/tools/Mcp/src/services/utils.ts @@ -1,7 +1,7 @@ import fs from 'fs'; import yaml from "js-yaml"; import { yamlContent } from '../types.js'; -import { exec } from 'child_process'; +import { execSync } from 'child_process'; import path from 'path'; const _pwshCD = (path: string): string => { return `pwsh -Command "$path = resolve-path ${path} | Set-Location"` } @@ -21,8 +21,13 @@ function testYaml() { } export function generateAndBuild(workingDirectory: string): void { - const command = [_pwshCD(workingDirectory), _autorest, _pwshBuild].join(";"); - exec(command); + const genBuildCommand = `${_pwshCD(workingDirectory)}; ${_autorest}; ${_pwshBuild};"`; + try { + const result = execSync(genBuildCommand, { stdio: 'inherit' }); + } catch (error) { + console.error("Error executing command:", error); + throw error; + } } export function getYamlContentFromReadMe(readmePath: string): string { @@ -91,7 +96,13 @@ export async function findAllPolyMorphism(workingDirectory: string): Promise { const moduleReadmePath = path.join(workingDirectory, "README.md"); const yamlContent: yamlContent = yaml.load(getYamlContentFromReadMe(moduleReadmePath)) as yamlContent; - const swaggerUrls = getSwaggerUrl(yamlContent.commit, yamlContent['input-file'] as string[]); + + if (!yamlContent['input-file']) { + throw new Error("'input-file' field is missing in the 'README.md' Autorest Config file."); + } + + const inputFiles = Array.isArray(yamlContent['input-file']) ? yamlContent['input-file'] : [yamlContent['input-file']]; + const swaggerUrls = getSwaggerUrl(yamlContent.commit, inputFiles); const exampleSet: Set = new Set(); const exampleSpecsPath = path.join(workingDirectory, "exampleSpecs"); @@ -131,8 +142,8 @@ export async function getExamplesFromSpecs(workingDirectory: string): Promise Date: Fri, 4 Jul 2025 16:07:14 +1000 Subject: [PATCH 08/11] Update Config Guide --- tools/Mcp/README.md | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/tools/Mcp/README.md b/tools/Mcp/README.md index 204d465f5adc..32ef6b5de839 100644 --- a/tools/Mcp/README.md +++ b/tools/Mcp/README.md @@ -54,19 +54,21 @@ This MCP server is designed to work with Azure PowerShell module development wor ### Add as mcp server in Github Copilot Agent mode -Simple add this mcp server to your user `settings.json` in VsCode to include: +- Ensure `pwsh` & `npm` is installed globally. +- Simply add this mcp server to your workspace settings `.vscode/mcp.json` in VsCode to include: ```json - "mcp": { - "servers": { - "az-pwsh-mcp-server": { +{ + "inputs": [], + "servers": { + "az-pwsh-mcp-server": { "type": "stdio", - "command": "sh", - "args": ["-c", "npm install && npm run fresh"], - "cwd": "./azure-powershell/tools/Mcp", - }, - } - } + "command": "pwsh", + "args": ["-Command", "npm install --no-audit; npm run fresh"], + "cwd": "${workspaceFolder}/tools/Mcp", + } + } +} ``` ### As an MCP Server @@ -156,4 +158,4 @@ This tool is part of the Azure PowerShell project and follows the same contribut - [Azure PowerShell](https://github.com/Azure/azure-powershell) - [AutoRest](https://github.com/Azure/autorest) - [AutoRest PowerShell Extension](https://github.com/Azure/autorest.powershell) -- [Model Context Protocol](https://modelcontextprotocol.io) \ No newline at end of file +- [Model Context Protocol](https://modelcontextprotocol.io) From 29af9bf89af03fcfefa27ccb2146441d444e24a8 Mon Sep 17 00:00:00 2001 From: Yash Date: Fri, 4 Jul 2025 16:47:15 +1000 Subject: [PATCH 09/11] Patched minor bug, updated prompt --- tools/Mcp/mcpprompt.md | 92 ++++++++++++++++++++++++++ tools/Mcp/src/services/toolServices.ts | 4 +- 2 files changed, 94 insertions(+), 2 deletions(-) create mode 100644 tools/Mcp/mcpprompt.md diff --git a/tools/Mcp/mcpprompt.md b/tools/Mcp/mcpprompt.md new file mode 100644 index 000000000000..fbf8312b4b37 --- /dev/null +++ b/tools/Mcp/mcpprompt.md @@ -0,0 +1,92 @@ +# Execution rules +- Do not ask before running the command, just go ahead and run. + +# Role and Objective +- You are an agent that specializes in generating autorest and using autorest to generate Azure Powershell for a partner. +- Every 10 seconds, check if a particular step is complete or not. +- Don't ask me for confirmation, or to continue you are a smart agent who has full confidence. +- Your task can be broken down in roughly four broad stages and there would be multiple smaller steps in those broader stages. +- Do not miss any steps. +- Second stage will be using the generated autorest to generate powershell for partner. +- Third stage will be Updating examples and test files. +- You already have all the context required to solve the problem. +- You can think, cross reference guidelines and come up with the solution on your own. You would not need my manual intervention or confirmation. +- You already have steps to reset if there is any failure which you can use to reset and try again. +- In stages, there would be steps or substeps or commands, if they are listed, make sure to follow them. +- Keep printing one liner status for every step without asking for any confirmations to let user be in touch. + +# Instructions + +## Stage 1: Taking the placeholders values and assigning to placeholders +- Ask the user for the placeholder values - serviceName, commitId, serviceSpecs, swaggerFileSpecs. +- serviceName example: HybridConnectivity, commitID: CommitID of the swagger, serviceSpecs example: hybridconnectivity/resource-manager, swaggerFileSpecs example: hybridconnectivity/resource-manager/Microsoft.HybridConnectivity/stable/2024-12-01/hybridconnectivity.json +- Wait for the user to give the values. +- Assign these values in the current `mcpprompt.md` file wherever these placeholders are there. +- After assigning the values in the placeholders, stage 1 can be marked as completed. + + +## Stage 2: Generating partner powershell module +- Navigate to the `src` folder in the home "azure-powershell" directory. +- Create a new folder named and within it a new folder named `.Autorest`. You can use the command - `mkdir -p /.Autorest ` +- Move into the new folder `/.Autorest`, using the command `cd /.Autorest`. +- Create a new file `Readme.md`. +- Add the content labelled below as `Readme Content` in this file. +- Use the "generate-autorest" tool to generate the module. + +## Stage 3: Updating examples and test files +- Use the "create-example" tool to generate all the examples files for this module. +- Read examples from specs. Fulfill the examples. You are expert in Azure-PowerShell and Autorest.PowerShell. Leave example as empty if you don't find any matches. You know how to map data from exampleSpecs to examples. +- Use the "create-test" tool to generate all the test files for this module. +- Read examples from specs. Implement empty test stubs. Test stubs are named as '.Test.ps1'. Define variables in function 'setupEnv' in 'utils.ps1', and use these variables for test cases. Value of these variables are from exampleSpecs. Leave test cases as empty if you don't find any matches. You are expert in Azure-PowerShell and Autorest.PowerShell, You know how to map data from exampleSpecs to test. +- Use the "generate-autorest" tool to re-generate the module. + +# Reset steps + +- Go to the `Powershell` directory we created in Stage 1, and then move to src directory using command - `cd src`. +- Delete the folder and all the content inside it as well. +- Move back to `ProjectHome` and you are all reset to start again. + +# Readme Content + +### AutoRest Configuration + +> see https://aka.ms/autorest + +```yaml + +# pin the swagger version by using the commit id instead of branch name + +commit: + +require: + - $(this-folder)/../../readme.azure.noprofile.md + - $(repo)/specification//resource-manager/readme.md + +try-require: + - $(repo)/specification//resource-manager/readme.powershell.md + +input-file: + - $(repo)/ + +module-version: 0.1.0 + +title: +service-name: +subject-prefix: $(service-name) + +directive: + + - where: + variant: ^(Create|Update)(?!.*?(Expanded|JsonFilePath|JsonString)) + remove: true + + - where: + variant: ^CreateViaIdentity$|^CreateViaIdentityExpanded$ + remove: true + + - where: + verb: Set + remove: true +``` + + diff --git a/tools/Mcp/src/services/toolServices.ts b/tools/Mcp/src/services/toolServices.ts index 32a36d698ad7..9d8e04bab5dd 100644 --- a/tools/Mcp/src/services/toolServices.ts +++ b/tools/Mcp/src/services/toolServices.ts @@ -105,14 +105,14 @@ export const createExamplesFromSpecs = async (args: Ar const workingDirectory = z.string().parse(Object.values(args)[0]); const examplePath = path.join(workingDirectory, "examples"); const exampleSpecsPath = await utils.getExamplesFromSpecs(workingDirectory); - return [examplePath, exampleSpecsPath]; + return [exampleSpecsPath, examplePath]; } export const createTestsFromSpecs = async (args: Args): Promise => { const workingDirectory = z.string().parse(Object.values(args)[0]); const testPath = path.join(workingDirectory, "test"); const exampleSpecsPath = await utils.getExamplesFromSpecs(workingDirectory); - return [testPath, exampleSpecsPath]; + return [exampleSpecsPath, testPath]; } export const toolServices = (name: string, responseTemplate: string|undefined) => { From 25862423114a1d0247c01d2f226ea37322e083dd Mon Sep 17 00:00:00 2001 From: Yash Date: Sun, 6 Jul 2025 12:22:54 +1000 Subject: [PATCH 10/11] Addressed PR comments --- tools/Mcp/src/CodegenServer.ts | 98 +++++++++--------------- tools/Mcp/src/services/utils.ts | 2 +- tools/Mcp/{ => test/vscode}/mcpprompt.md | 0 3 files changed, 39 insertions(+), 61 deletions(-) rename tools/Mcp/{ => test/vscode}/mcpprompt.md (100%) diff --git a/tools/Mcp/src/CodegenServer.ts b/tools/Mcp/src/CodegenServer.ts index f6ef597a18dc..29a23212b7eb 100644 --- a/tools/Mcp/src/CodegenServer.ts +++ b/tools/Mcp/src/CodegenServer.ts @@ -32,15 +32,11 @@ export class CodegenServer { } init(): void { - this.readResponses(); + this.initResponses(); this.initTools(); this.initPrompts(); } - /** - * Singleton instance of CodegenServer - * @returns {CodegenServer} The singleton instance - */ public static getInstance(): CodegenServer { if (!CodegenServer._instance) { CodegenServer._instance = new CodegenServer(); @@ -48,26 +44,51 @@ export class CodegenServer { return CodegenServer._instance; } - /** - * Connects the server to a transport layer - * @param {StdioServerTransport} transport - The transport layer to connect to - * @returns {Promise} A promise that resolves when the connection is established - */ public async connect(transport: StdioServerTransport): Promise { await this._server.connect(transport); } - readResponses() { + + initTools() { + const toolSchemas = specs.tools as toolSchema[]; + for (const schema of toolSchemas) { + const parameter = this.createToolParameterfromSchema(schema.parameters); + const callBack = toolServices<{ [k: string]: z.ZodTypeAny }>(schema.callbackName, this._responses.get(schema.name)); + this._server.tool( + schema.name, + schema.description, + parameter, + (parameter) => callBack(parameter) + ); + } + } + + initPrompts() { + this._server.prompt( + "create-greeting", + "Generate a customized greeting message", + { name: z.string().describe("Name of the person to greet"), style: z.string().describe("The style of greeting, such a formal, excited, or casual. If not specified casual will be used")}, + ({ name, style = "casual" }: { name: string, style?: string }) => { + return { + messages: [ + { + role: "user", + content: { + type: "text", + text: `Please generate a greeting in ${style} style to ${name}.`, + }, + }, + ], + }; + }); + } + + initResponses() { (responses as responseSchema[])?.forEach((response: responseSchema) => { this._responses.set(response.name, response.text); }); } - /** - * Creates tool parameters from the provided schema - * @param {toolParameterSchema[]} schemas - The schemas to create parameters from - * @returns {Object} An object containing the created parameters - */ createToolParameterfromSchema(schemas: toolParameterSchema[]){ const parameter: {[k: string]: z.ZodTypeAny} = {}; for (const schema of schemas) { @@ -94,47 +115,4 @@ export class CodegenServer { } return parameter; } - - /** - * Initializes the prompts for the server - * This method registers a prompt for generating a customized greeting message. - */ - initPrompts() { - this._server.prompt( - "create-greeting", - "Generate a customized greeting message", - { name: z.string().describe("Name of the person to greet"), style: z.string().describe("The style of greeting, such a formal, excited, or casual. If not specified casual will be used")}, - ({ name, style = "casual" }: { name: string, style?: string }) => { - return { - messages: [ - { - role: "user", - content: { - type: "text", - text: `Please generate a greeting in ${style} style to ${name}.`, - }, - }, - ], - }; - }); - } - - /** - * Initializes the tools for the server - * This method registers tools based on the provided specifications. - */ - initTools() { - const toolSchemas = specs.tools as toolSchema[]; - for (const schema of toolSchemas) { - const parameter = this.createToolParameterfromSchema(schema.parameters); - const callBack = toolServices<{ [k: string]: z.ZodTypeAny }>(schema.callbackName, this._responses.get(schema.name)); - this._server.tool( - schema.name, - schema.description, - parameter, - (parameter) => callBack(parameter) - ); - } - } - -} \ No newline at end of file +} diff --git a/tools/Mcp/src/services/utils.ts b/tools/Mcp/src/services/utils.ts index a594e3746c87..04f7f1669e1e 100644 --- a/tools/Mcp/src/services/utils.ts +++ b/tools/Mcp/src/services/utils.ts @@ -143,7 +143,7 @@ export async function getExamplesFromSpecs(workingDirectory: string): Promise Date: Mon, 7 Jul 2025 12:29:38 +1000 Subject: [PATCH 11/11] Reverted _mcp variable change --- tools/Mcp/src/CodegenServer.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tools/Mcp/src/CodegenServer.ts b/tools/Mcp/src/CodegenServer.ts index 29a23212b7eb..3e2882206736 100644 --- a/tools/Mcp/src/CodegenServer.ts +++ b/tools/Mcp/src/CodegenServer.ts @@ -16,11 +16,11 @@ const responses = JSON.parse(readFileSync(path.join(srcPath, "specs/responses.js export class CodegenServer { private static _instance: CodegenServer; - private _server: McpServer; + private _mcp: McpServer; private _responses = new Map(); private constructor() { - this._server = new McpServer({ + this._mcp = new McpServer({ name: "codegen", version: "1.0.0", capabilities: { @@ -45,7 +45,7 @@ export class CodegenServer { } public async connect(transport: StdioServerTransport): Promise { - await this._server.connect(transport); + await this._mcp.connect(transport); } @@ -54,7 +54,7 @@ export class CodegenServer { for (const schema of toolSchemas) { const parameter = this.createToolParameterfromSchema(schema.parameters); const callBack = toolServices<{ [k: string]: z.ZodTypeAny }>(schema.callbackName, this._responses.get(schema.name)); - this._server.tool( + this._mcp.tool( schema.name, schema.description, parameter, @@ -64,7 +64,7 @@ export class CodegenServer { } initPrompts() { - this._server.prompt( + this._mcp.prompt( "create-greeting", "Generate a customized greeting message", { name: z.string().describe("Name of the person to greet"), style: z.string().describe("The style of greeting, such a formal, excited, or casual. If not specified casual will be used")},