Skip to content

Commit f998c27

Browse files
notyashhhVeryEarlyCopilotisra-fel
authored
[MCP] Sync Code Generation MCP server to main branch (#28703)
Co-authored-by: Yabo Hu <[email protected]> Co-authored-by: Copilot <[email protected]> Co-authored-by: Yeming Liu <[email protected]>
1 parent bd61a36 commit f998c27

40 files changed

+19077
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ launchSettings.json
233233
/tools/Modules/tmp
234234
/tools/Az/Az.psm1
235235
/tools/AzPreview/AzPreview.psm1
236+
/tools/Mcp/.logs
236237
/Azure.PowerShell.sln
237238

238239
# Added due to scan

tools/Mcp/README.md

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
# Azure PowerShell Codegen MCP Server
2+
3+
A Model Context Protocol (MCP) server that provides tools for generating and managing Azure PowerShell modules using AutoRest. It now also orchestrates help‑driven example generation, CRUD test scaffolding, and an opinionated partner workflow to keep outputs deterministic and consistent.
4+
5+
## Overview
6+
7+
This MCP server is designed to work with Azure PowerShell module development workflows. It provides specialized tools for:
8+
9+
- **Module Scaffolding**: Interactive selection of service → provider → API version and creation of the `<ModuleName>.Autorest` structure
10+
- **AutoRest Code Generation**: Generate PowerShell modules from OpenAPI specifications (reset/generate/build sequence)
11+
- **Example Generation**: Create example scripts from swagger example JSON while filtering strictly to parameters documented in help markdown
12+
- **Test Generation**: Produce per‑resource CRUD test files (idempotent, includes negative test) using the same help‑driven parameter filtering
13+
- **Help‑Driven Parameter Filtering**: Only parameters present in the generated help (`/src/<Module>/help/*.md`) are allowed in examples/tests
14+
- **Model Management**: Handle model directives like `no-inline` and `model-cmdlet`
15+
- **Polymorphism Support**: Automatically detect and configure parent/child discriminator relationships
16+
- **YAML Configuration Utilities**: Parse and manipulate AutoRest configuration blocks
17+
- **Partner Workflow Prompt**: A single prompt that encodes the end‑to‑end deterministic workflow
18+
19+
## Features
20+
21+
### Available Tools
22+
23+
1. **setup-module-structure**
24+
- Interactive service → provider → API version selection and module name capture
25+
- Scaffolds `src/<Module>/<Module>.Autorest/` plus initial `README.md`
26+
- Output placeholder `{0}` = module name
27+
28+
2. **generate-autorest**
29+
- Executes Autorest reset, generate, and PowerShell build steps within the given working directory
30+
- Parameters: `workingDirectory` (absolute path to the Autorest folder containing README.md)
31+
- Output placeholder `{0}` = working directory
32+
33+
3. **create-example**
34+
- Downloads swagger example JSON, filters parameters to those documented in help markdown (`/src/<Module>/help/<Cmdlet>.md`), and writes example scripts under `examples/`
35+
- Parameters: `workingDirectory`
36+
- Output placeholders: `{0}` = harvested specs path, `{1}` = examples dir, `{2}` = reference ideal example dirs
37+
38+
4. **create-test**
39+
- Generates new `<ResourceName>.Crud.Tests.ps1` files (does not modify stubs) with Create/Get/List/Update/Delete/Negative blocks, using help‑filtered parameters
40+
- Parameters: `workingDirectory`
41+
- Output placeholders: `{0}` = harvested specs path, `{1}` = test dir, `{2}` = reference ideal test dirs
42+
43+
5. **polymorphism**
44+
- Detects discriminator parents and child model names to aid directive insertion
45+
- Parameters: `workingDirectory`
46+
- Output placeholders: `{0}` = parents, `{1}` = children, `{2}` = working directory
47+
48+
6. **no-inline**
49+
- Lists models to be marked `no-inline` (caller inserts directive into README Autorest YAML)
50+
- Parameters: `modelNames` (array)
51+
- Output `{0}` = comma-separated model list
52+
53+
7. **model-cmdlet**
54+
- Lists models for which `New-` object construction cmdlets should be added via directives
55+
- Parameters: `modelNames` (array)
56+
- Output `{0}` = comma-separated model list
57+
58+
### Available Prompts
59+
60+
- **partner-module-workflow**: Canonical end‑to‑end instruction set (module structure → generation → examples → tests → regeneration)
61+
- **create-greeting**: Sample/demo greeting prompt
62+
63+
## Installation
64+
65+
1. Clone or download this repository
66+
2. Install dependencies:
67+
```bash
68+
npm install
69+
```
70+
3. Build the TypeScript code:
71+
```bash
72+
npm run build
73+
```
74+
75+
## Usage
76+
77+
### Add as mcp server in Github Copilot Agent mode
78+
79+
- Ensure `pwsh` & `npm` is installed globally.
80+
- Simply add this mcp server to your workspace settings `.vscode/mcp.json` in VsCode to include:
81+
82+
```json
83+
{
84+
"inputs": [],
85+
"servers": {
86+
"az-pwsh-mcp-server": {
87+
"type": "stdio",
88+
"command": "pwsh",
89+
"args": ["-Command", "npm install --no-audit; npm run fresh"],
90+
"cwd": "${workspaceFolder}/tools/Mcp",
91+
}
92+
}
93+
}
94+
```
95+
96+
### As an MCP Server
97+
98+
The server runs on stdio and can be integrated with MCP-compatible clients:
99+
100+
```bash
101+
node build/index.js
102+
```
103+
104+
### Direct Integration
105+
106+
You can also use the server programmatically:
107+
108+
```typescript
109+
import { CodegenServer } from './src/CodegenServer.js';
110+
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
111+
112+
const server = CodegenServer.getInstance();
113+
server.init();
114+
const transport = new StdioServerTransport();
115+
await server.connect(transport);
116+
```
117+
118+
## Configuration
119+
120+
The server uses JSON configuration files for tool definitions:
121+
122+
- `src/specs/specs.json`: Tool specifications and parameters
123+
- `src/specs/responses.json`: Response templates for tools
124+
125+
## Example Workflow
126+
127+
1. **Detect Polymorphism**: Use the `polymorphism` tool to identify parent-child type relationships
128+
2. **Configure Models**: Apply `no-inline` directives for parent types
129+
3. **Create Cmdlets**: Use `model-cmdlet` to generate creation cmdlets for child types
130+
4. **Generate Code**: Run `generate-autorest` to build the PowerShell module
131+
132+
## Development
133+
134+
### Project Structure
135+
136+
```
137+
src/
138+
├── CodegenServer.ts # Main MCP server implementation
139+
├── index.ts # Entry point
140+
├── types.ts # TypeScript type definitions
141+
├── services/
142+
│ ├── toolServices.ts # Tool implementation logic
143+
│ ├── utils.ts # Utility functions
144+
│ └── ...
145+
└── specs/
146+
├── specs.json # Tool specifications
147+
└── responses.json # Response templates
148+
```
149+
150+
### Building
151+
152+
```bash
153+
npm run build
154+
```
155+
156+
This compiles TypeScript files to the `build/` directory.
157+
158+
## Dependencies
159+
160+
- **@modelcontextprotocol/sdk**: Core MCP functionality
161+
- **js-yaml**: YAML parsing for AutoRest configuration
162+
- **zod**: Runtime type validation and schema definition
163+
164+
## Requirements
165+
166+
- Node.js (ES modules support)
167+
- TypeScript 5.8+
168+
- AutoRest (for code generation functionality)
169+
170+
## Contributing
171+
172+
This tool is part of the Azure PowerShell project and follows the same contribution guidelines. Please ensure all changes maintain compatibility with the existing Azure PowerShell development workflow.
173+
174+
## Related Projects
175+
176+
- [Azure PowerShell](https://github.com/Azure/azure-powershell)
177+
- [AutoRest](https://github.com/Azure/autorest)
178+
- [AutoRest PowerShell Extension](https://github.com/Azure/autorest.powershell)
179+
- [Model Context Protocol](https://modelcontextprotocol.io)

0 commit comments

Comments
 (0)