Repository navigation
Refactor api2ai into modular architecture for mcp-use 2.x - #76
Merged
Merged
Conversation
…use 2.x
The 1,000-line generate-mcp-use-server.js is now spec/ (load, OpenAPI → Zod,
risk, tool extraction), templates/ (one module per generated file),
generate.js, cli.js and index.js. The old path remains as a re-exporting shim.
Generated servers now target mcp-use ^2.7.3 + zod 4: `import { MCPServer }
from 'mcp-use'`, `inputSchema`, MCP tool annotations, awaited listen(), raw
tool results with isError/structuredContent, and `mcp-use dev` for the
Inspector. dotenv is replaced by `node --env-file-if-exists`.
Fixes along the way:
- descriptions/enums with quotes, newlines or backslashes broke the output
- base URL path was dropped (`/api/v3` + `/pet` → `/pet`)
- relative and templated `servers[].url` are resolved
- local $ref schemas/parameters/bodies, allOf, 3.0/3.1 nullable
- --approve-writes was parsed but never applied
- duplicate tool names and duplicate path/operation params
- host allowlist now defaults to the spec's API host, as documented
- response size cap is enforced while streaming, in bytes
- regenerating no longer overwrites an existing .env
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01RDDWQMAX1u1DQcFHrE5hFY
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
This branch had an error being deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Refactored the monolithic
generate-mcp-use-server.jsinto a modular architecture with separate concerns for spec loading, schema conversion, tool extraction, code generation, and CLI handling. Updated generated servers to target mcp-use 2.x API with zod 4 support.Key Changes
Modularized codebase: Split 1000+ line single file into focused modules:
spec/load-spec.js— fetch and parse OpenAPI specs (JSON/YAML)spec/schema-to-zod.js— convert OpenAPI schemas to Zod source code with proper escapingspec/extract-tools.js— extract operations into tool descriptionsspec/classify-risk.js— categorize tools by risk level with MCP annotationstemplates/*— one module per generated file (package.json, .env, policy, http-client, etc.)cli.js— command-line argument parsing and entry pointgenerate.js— orchestrate spec loading and file renderingindex.js— library exportsmcp-use 2.x compatibility:
MCPServerclass andinputSchema(notschema)readOnlyHint,destructiveHint,idempotentHintderived from HTTP methodlisten()instead of synchronous server setupz.iso.datetime(),z.email(),z.uuid()formattersImproved schema handling:
literal()function — prevents injection of newlines, backslashes,*/into generated code$refresolution with cycle detection (cycles becomez.unknown())allOfmerging for plain object schemasnullable: true) and 3.1 (type: ['string', 'null']) nullability supportEnhanced HTTP client:
/api/v3)Better documentation:
Test coverage: Added comprehensive test suite (
packages/grab-url/test/api2ai.test.ts) covering schema conversion, tool extraction, CLI parsing, and generated file validityImplementation Details
JSON.stringify()before splicing into generated sourcegenerate-mcp-use-server.jsre-exports from new moduleshttps://claude.ai/code/session_01RDDWQMAX1u1DQcFHrE5hFY