# Directory Structure
```
├── .gitignore
├── LICENSE
├── package-lock.json
├── package.json
├── README.md
├── src
│ └── index.ts
└── tsconfig.json
```
# Files
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
```
1 | node_modules/
2 | build/
3 | *.log
4 | .env*
```
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
```markdown
1 | # openapi-client-generator MCP Server
2 |
3 | A Model Context Protocol server to generate API clients by using TypeScript.
4 |
5 |
6 | <a href="https://glama.ai/mcp/servers/taqmq8493y">
7 | <img width="380" height="200" src="https://glama.ai/mcp/servers/taqmq8493y/badge" alt="OpenAPI Client Generator MCP server" />
8 | </a>
9 |
10 | ## Features
11 |
12 | ### Resources
13 | - Generates an axios based API client that can be used to interact with the API.
14 | - It uses OpenAPI / Swagger specs to generate the client.
15 |
16 | ### Prompts
17 | - `generate_client` - Generate a API client for specified OpenAPI specs.
18 |
19 | ## Development
20 |
21 | Install dependencies:
22 | ```bash
23 | npm install
24 | ```
25 |
26 | Build the server:
27 | ```bash
28 | npm run build
29 | ```
30 |
31 | For development with auto-rebuild:
32 | ```bash
33 | npm run watch
34 | ```
35 |
36 | ## Installation
37 |
38 | To use with Claude Desktop, add the server config:
39 |
40 | On MacOS: `~/Library/Application Support/Claude/claude_desktop_config.json`
41 | On Windows: `%APPDATA%/Claude/claude_desktop_config.json`
42 |
43 | ```json
44 | {
45 | "mcpServers": {
46 | "openapi-client-generator": {
47 | "command": "node",
48 | "args": [
49 | "< PATH TO >/openapi-client-generator/build/index.js"
50 | ]
51 | }
52 | }
53 | }
54 | ```
55 |
56 | ### Debugging
57 |
58 | Since MCP servers communicate over stdio, debugging can be challenging. We recommend using the [MCP Inspector](https://github.com/modelcontextprotocol/inspector), which is available as a package script:
59 |
60 | ```bash
61 | npm run inspector
62 | ```
63 |
64 | The Inspector will provide a URL to access debugging tools in your browser.
65 |
66 | ## Contributing
67 |
68 | Feel free to contribute to the project by opening issues or submitting pull requests. We welcome any improvements or new features that align with the project's goals.
69 |
70 | ## License
71 |
72 | This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
73 |
74 | ----
75 |
76 | Built with [Cline](https://github.com/cline/cline)
77 |
```
--------------------------------------------------------------------------------
/tsconfig.json:
--------------------------------------------------------------------------------
```json
1 | {
2 | "compilerOptions": {
3 | "target": "ES2022",
4 | "module": "Node16",
5 | "moduleResolution": "Node16",
6 | "outDir": "./build",
7 | "rootDir": "./src",
8 | "strict": true,
9 | "esModuleInterop": true,
10 | "skipLibCheck": true,
11 | "forceConsistentCasingInFileNames": true
12 | },
13 | "include": ["src/**/*"],
14 | "exclude": ["node_modules"]
15 | }
16 |
```
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
```json
1 | {
2 | "name": "openapi-client-generator",
3 | "version": "0.1.1",
4 | "description": "A Model Context Protocol server to generate API clients by using TypeScript.",
5 | "private": true,
6 | "type": "module",
7 | "bin": {
8 | "openapi-client-generator": "./build/index.js"
9 | },
10 | "files": [
11 | "build"
12 | ],
13 | "scripts": {
14 | "build": "tsc && node -e \"require('fs').chmodSync('build/index.js', '755')\"",
15 | "prepare": "npm run build",
16 | "watch": "tsc --watch",
17 | "inspector": "npx @modelcontextprotocol/inspector build/index.js"
18 | },
19 | "dependencies": {
20 | "@modelcontextprotocol/sdk": "0.6.0",
21 | "@types/node-fetch": "^2.6.12",
22 | "axios": "^1.7.9",
23 | "node-fetch": "^2.7.0",
24 | "openapi-typescript-codegen": "^0.29.0"
25 | },
26 | "devDependencies": {
27 | "@types/node": "^20.11.24",
28 | "typescript": "^5.3.3"
29 | }
30 | }
```
--------------------------------------------------------------------------------
/src/index.ts:
--------------------------------------------------------------------------------
```typescript
1 | #!/usr/bin/env node
2 | import { Server } from "@modelcontextprotocol/sdk/server/index.js";
3 | import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
4 | import {
5 | CallToolRequestSchema,
6 | ErrorCode,
7 | ListToolsRequestSchema,
8 | McpError,
9 | } from "@modelcontextprotocol/sdk/types.js";
10 | import { generate } from "openapi-typescript-codegen";
11 | import fs from "fs/promises";
12 |
13 | interface GenerateClientArgs {
14 | input: string;
15 | output: string;
16 | httpClient: "fetch" | "axios";
17 | }
18 |
19 | class OpenApiClientGenerator {
20 | private server: Server;
21 |
22 | constructor() {
23 | this.server = new Server(
24 | {
25 | name: "openapi-client-generator",
26 | version: "0.1.1",
27 | },
28 | {
29 | capabilities: {
30 | tools: {},
31 | },
32 | }
33 | );
34 |
35 | this.setupToolHandlers();
36 |
37 | this.server.onerror = (error) => console.error("[MCP Error]", error);
38 | process.on("SIGINT", async () => {
39 | await this.server.close();
40 | process.exit(0);
41 | });
42 | }
43 |
44 | private setupToolHandlers() {
45 | this.server.setRequestHandler(ListToolsRequestSchema, async () => ({
46 | tools: [
47 | {
48 | name: "generate_client",
49 | description:
50 | "Generate TypeScript API client from OpenAPI specification",
51 | inputSchema: {
52 | type: "object",
53 | properties: {
54 | input: {
55 | type: "string",
56 | description: "URL or file path to OpenAPI specification",
57 | },
58 | output: {
59 | type: "string",
60 | description: "Output directory for generated client",
61 | },
62 | httpClient: {
63 | type: "string",
64 | enum: ["fetch", "axios"],
65 | description: "HTTP client to use (fetch or axios)",
66 | },
67 | },
68 | required: ["input", "output", "httpClient"],
69 | additionalProperties: false,
70 | },
71 | },
72 | ],
73 | }));
74 |
75 | this.server.setRequestHandler(CallToolRequestSchema, async (request) => {
76 | if (request.params.name !== "generate_client") {
77 | throw new McpError(
78 | ErrorCode.MethodNotFound,
79 | `Unknown tool: ${request.params.name}`
80 | );
81 | }
82 |
83 | const args = request.params.arguments as unknown as GenerateClientArgs;
84 |
85 | try {
86 | // Ensure output directory exists
87 | await fs.mkdir(args.output, { recursive: true });
88 |
89 | // Generate client
90 | await generate({
91 | input: args.input,
92 | output: args.output,
93 | httpClient: args.httpClient,
94 | useOptions: true,
95 | useUnionTypes: true,
96 | exportSchemas: true,
97 | exportServices: true,
98 | exportCore: true,
99 | indent: "2",
100 | });
101 |
102 | // Get list of generated files
103 | const files = await fs.readdir(args.output);
104 |
105 | return {
106 | content: [
107 | {
108 | type: "text",
109 | text: `Successfully generated TypeScript API client in ${
110 | args.output
111 | }\n\nGenerated files:\n${files.join("\n")}`,
112 | },
113 | ],
114 | };
115 | } catch (error) {
116 | return {
117 | content: [
118 | {
119 | type: "text",
120 | text: `Error generating client: ${
121 | error instanceof Error ? error.message : String(error)
122 | }`,
123 | },
124 | ],
125 | isError: true,
126 | };
127 | }
128 | });
129 | }
130 |
131 | async run() {
132 | const transport = new StdioServerTransport();
133 | await this.server.connect(transport);
134 | console.error("OpenAPI Client Generator MCP server running on stdio");
135 | }
136 | }
137 |
138 | const server = new OpenApiClientGenerator();
139 | server.run().catch(console.error);
140 |
```