Skip to content

Enterprise-Grade MCP Server Generation from OpenAPI Specifications

License

Notifications You must be signed in to change notification settings

Matt-MFG/Magic-MCP

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

9 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿช„ Magic MCP

Enterprise-Grade MCP Server Generation from OpenAPI Specifications

Transform any OpenAPI specification into a production-ready, fully-typed TypeScript MCP server with AI-powered code generation and built-in security scanning.

License: MIT TypeScript Node.js Phase

๐ŸŒŸ What is Magic MCP?

Magic MCP automatically generates Model Context Protocol (MCP) servers from OpenAPI specifications, producing:

  • โœ… Fully-typed TypeScript code with proper response types and interfaces
  • โœ… Runtime validation using Zod schemas with proper array types (z.array(z.string()))
  • โœ… Automated test generation with Vitest (26 passing tests for GitHub API)
  • โœ… OpenAPI component names preserved (Repository not NestedType1)
  • โœ… Deduplicated type definitions using TypeScript type aliases (40-60% code reduction)
  • โœ… Security scanning with 99/100 average score
  • โœ… AI-powered analysis using Vertex AI Gemini for schema insights

๐ŸŽฏ Quick Start

Prerequisites

  • Node.js 20+
  • npm 10+
  • Google Cloud account (for AI features)

Installation

# Clone repository
git clone https://github.com/YOUR_USERNAME/Magic-MCP.git
cd Magic-MCP

# Install dependencies
npm install

# Build all packages
npm run build

Configuration

Create a .env file:

GOOGLE_CLOUD_PROJECT=your-project-id
GOOGLE_CLOUD_LOCATION=us-central1
VERTEX_AI_MODEL=gemini-2.0-flash-exp
VERTEX_AI_LOCATION=us-central1
LOG_LEVEL=info

Configure Google Cloud:

gcloud config configurations create magic-mcp
gcloud auth login
gcloud config set project your-project-id
gcloud services enable aiplatform.googleapis.com
gcloud auth application-default login

Generate Your First MCP Server

# Generate from OpenAPI spec with tests
node packages/cli/dist/cli.js generate \
  examples/github-repos-api.yaml \
  --output my-mcp-server

# Build, test, and run the generated server
cd my-mcp-server
npm install
npm run build
npm test        # Run 26 generated tests!
npm start

๐Ÿš€ Features

Current (Phase 2F Complete)

Code Generation

  • โœ… OpenAPI 2.0, 3.0, 3.1 support
  • โœ… TypeScript ES Modules with NodeNext resolution
  • โœ… Zod runtime validation with proper array types (z.array(z.string()))
  • โœ… Bearer and API Key authentication
  • โœ… Path, query, and body parameter handling
  • โœ… Request body support (JSON + form-urlencoded)

Type System

  • โœ… Response type generation with TypeScript interfaces
  • โœ… Nested schema extraction (3+ properties)
  • โœ… Global type deduplication (40-60% code reduction)
  • โœ… OpenAPI component name preservation
  • โœ… Array item type extraction with Zod schemas
  • โœ… JSDoc comments from OpenAPI descriptions

Testing & Quality

  • โœ… Automated Vitest test generation (26 tests for GitHub API)
  • โœ… Input validation tests for all endpoints
  • โœ… Parameter handling tests (path, query, body)
  • โœ… Error handling and type safety tests
  • โœ… AI-powered schema analysis (Vertex AI Gemini)
  • โœ… Security scanning (10+ vulnerability categories, 99/100 average score)
  • โœ… Circular reference handling
  • โœ… Prettier code formatting
  • โœ… TypeScript strict mode compilation (100% success rate)

Example Generated Code

Input: OpenAPI spec with components/schemas/Repository

Output:

// Component name preserved!
export interface Repository {
  /** Unique identifier of the repository */
  id: number;
  /** The name of the repository */
  name: string;
  /** Simple User */
  owner: { login: string; id: number };
  // ... 20 properties with JSDoc
}

// Array response uses the component type
export type ListForAuthenticatedUserResponse = Repository[];

// Duplicate responses use type aliases (no duplication!)
export type CreateForAuthenticatedUserResponse = Repository;
export type GetResponse = Repository;
export type UpdateResponse = Repository;

// Zod schema for nested type (for validation)
export const RepositorySchema = z.object({
  id: z.number().describe('Unique identifier'),
  name: z.string().describe('The name of the repository'),
  owner: z.object({ login: z.string(), id: z.number() }),
  // ... full validation
});

// Zod validation with enum and array support
const ReposListSchema = z.object({
  visibility: z.enum(['all', 'public', 'private']).optional(),
  tags: z.array(z.string()).optional(),  // Proper array types!
  ids: z.array(z.number()).optional(),
});

// Fully-typed client method
async reposListForAuthenticatedUser(
  params: z.infer<typeof ReposListSchema>
): Promise<ListForAuthenticatedUserResponse> {
  // Implementation with type safety
}

// Generated tests (26 tests for GitHub API)
describe('repos_list_for_authenticated_user', () => {
  it('should validate input schema correctly', () => { ... });
  it('should build query parameters correctly', () => { ... });
  // ... 24 more tests
});

๐Ÿ“Š Generation Quality

Tested With:

  • โœ… Petstore API (basic CRUD)
  • โœ… GitHub API (1,350 endpoints)
  • โœ… Stripe API (572 endpoints, 6.8MB spec)

Quality Metrics:

  • TypeScript compilation: 100% success rate
  • Security score: 99/100 average
  • Code reduction: 40-60% via deduplication
  • Generation time: < 15s for typical APIs

๐Ÿ—๏ธ Architecture

packages/
โ”œโ”€โ”€ cli/              # Command-line interface (Commander.js)
โ”œโ”€โ”€ parser/           # OpenAPI parser (@apidevtools/swagger-parser)
โ”œโ”€โ”€ generator/        # Code generator (Handlebars + Vertex AI)
โ”œโ”€โ”€ security/         # Security scanner (10+ categories)
โ””โ”€โ”€ shared/           # Shared types and utilities

Data Flow:

OpenAPI Spec โ†’ Parser โ†’ AI Analysis โ†’ MCP Spec โ†’ Template โ†’ Generated Code โ†’ Security Scan

๐Ÿ”’ Security

Magic MCP generates secure code by default:

  • Input Validation: Zod schemas validate all inputs
  • Type Safety: Full TypeScript strict mode
  • Authentication: Bearer token and API key support
  • Security Scanning: Automatic vulnerability detection
  • Best Practices: Generated code follows MCP SDK patterns

Security Categories Checked:

  • Missing authentication details
  • Insufficient input validation
  • Missing authorization checks
  • Lack of rate limiting
  • Potential information disclosure
  • Missing audit logging
  • XSS vulnerabilities
  • CSRF protection

๐Ÿ“š Documentation

Getting Started:

Phase Documentation:

๐Ÿ“ฆ Packages

Package Description Status
@magic-mcp/cli Command-line interface โœ… Working
@magic-mcp/generator AI-powered code generator โœ… Working
@magic-mcp/parser OpenAPI schema parser โœ… Working
@magic-mcp/security Security scanning โœ… Working
@magic-mcp/shared Shared types and utilities โœ… Working

๐Ÿ—บ๏ธ Roadmap

โœ… Phase 1: Foundation (Complete)

  • โœ… Monorepo setup with Turborepo
  • โœ… OpenAPI parser (v2.0, v3.0, v3.1)
  • โœ… Template-based code generation
  • โœ… CLI tool

โœ… Phase 2A: AI Generation Quality (Complete)

  • โœ… Proper Zod type mapping
  • โœ… Path parameter replacement
  • โœ… Class name sanitization
  • โœ… Request body extraction

โœ… Phase 2B: Real-World API Testing (Complete)

  • โœ… Stripe API (572 endpoints)
  • โœ… GitHub API (1,350 endpoints)
  • โœ… Enum support in Zod schemas
  • โœ… Proper OpenAPI descriptions

โœ… Phase 2C: Response Types & Advanced Features (Complete)

  • โœ… TypeScript response type generation
  • โœ… Nested schema extraction (3+ properties)
  • โœ… Response type deduplication
  • โœ… OpenAPI component name preservation
  • โœ… Array item type extraction
  • โœ… Circular reference handling

๐Ÿ”„ Phase 2D: Next Steps (Planned)

  • Zod array schemas (z.array(ItemSchema))
  • Test generation (Vitest)
  • Multi-cloud deployment (Cloud Run, Lambda, Workers)

๐Ÿ”ฎ Phase 3: Intelligence Platform (Future)

  • MCP discovery and search
  • Gap analysis tools
  • Feature suggestions and improvements

See claude.md for detailed development priorities.

๐Ÿค Contributing

We welcome contributions! Please see CONTRIBUTING.md for:

  • Setup instructions
  • Testing & evaluation criteria
  • Pull request process
  • Bug report templates
  • Feature request guidelines

Quick Testing:

npm run build
node packages/cli/dist/cli.js generate examples/github-repos-api.yaml --output test
cd test && npm run build  # Should compile without errors

๐Ÿ’ป CLI Reference

# Generate MCP server
node packages/cli/dist/cli.js generate <spec-path> [options]

Options:
  --output <dir>     Output directory (default: ./output)
  --name <name>      Custom MCP server name
  --no-tests         Skip test generation
  --project <id>     Google Cloud project ID
  --location <loc>   Vertex AI location (default: us-central1)

# Examples
node packages/cli/dist/cli.js generate https://api.stripe.com/openapi.yaml
node packages/cli/dist/cli.js generate examples/github-repos-api.yaml --output my-server
node packages/cli/dist/cli.js generate spec.json --no-tests --name my-api

๐Ÿงช Testing

Run the evaluation suite from CONTRIBUTING.md:

# 1. Code Generation Quality
npm run build
node packages/cli/dist/cli.js generate examples/github-repos-api.yaml --output test
cd test && npm run build  # Should succeed

# 2. Component Name Preservation
grep "export interface Repository" test/src/index.ts  # Should find

# 3. Type Safety
grep -c "Promise<unknown>" test/src/index.ts  # Should be minimal

# 4. Security Score
# Check CLI output for "Security scan passed (score: XX/100)"

๐Ÿ“Š Performance

Build Time: ~1.5s (with Turborepo caching)

Generation Time:

  • Simple API (5 endpoints): ~10s
  • Medium API (50 endpoints): ~15s
  • Large API (500 endpoints): ~30s

Generated Code Size:

  • Typical API: 10-30KB TypeScript
  • Large API: 50-100KB TypeScript
  • 40-60% reduction via deduplication

๐Ÿ™ Acknowledgments

Built with:

๐Ÿ“„ License

MIT License - see LICENSE for details.

๐Ÿ“ž Contact


Current Status: Phase 2C Complete - Production-ready TypeScript MCP generation with full type safety, component name preservation, and enterprise-grade security scanning.

Made with โค๏ธ and Claude Code

About

Enterprise-Grade MCP Server Generation from OpenAPI Specifications

Resources

License

Contributing

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •