Skip to content

shawngustaw/mcp-to-typescript-codegen

Repository files navigation

mcp-to-typescript-codegen

Generate TypeScript types from MCP (Model Context Protocol) servers.

Installation

npm install -g mcp-to-typescript-codegen

Or use with npx:

npx mcp-to-typescript-codegen --command "your-mcp-server"

Usage

Stdio Transport (Local Server)

# Basic usage
mcp-to-typescript-codegen --command "mcp-server"

# With arguments
mcp-to-typescript-codegen --command "npx" --args "@modelcontextprotocol/server-filesystem,/tmp"

# Custom output file
mcp-to-typescript-codegen --command "mcp-server" --output "./types/mcp.ts"

# With namespace prefix (for multiple servers)
mcp-to-typescript-codegen --command "apollo-mcp" --name Apollo --output "./types/apollo.ts"

HTTP Transport (Remote Server)

mcp-to-typescript-codegen --server "http://localhost:3000/mcp"

Options

Flag Short Description
--command <cmd> -c Command to spawn MCP server (stdio transport)
--args <args> -a Comma-separated arguments for the command
--server <url> -s URL of HTTP MCP server (streamable HTTP transport)
--output <file> -o Output file path (default: generated/mcp-tools.ts)
--name <prefix> -n Prefix for generated type names (e.g., "Apollo")
--debug -d Print raw tool schemas from server
--help -h Show help

Generated Output

// Auto-generated by mcp-to-typescript-codegen
// Do not edit manually

export type ReadFileParams = {
  path: string;
};

export type ListDirectoryParams = {
  path: string;
};

export type ToolName = "read_file" | "list_directory";

export const toolNames = ["read_file", "list_directory"] as const;

With --name Apollo:

export type ApolloExecuteParams = {
  query: string;
  variables?: string;
};

export type ApolloToolName = "execute" | "introspect";

export const apolloToolNames = ["execute", "introspect"] as const;

Using Generated Types

import { ReadFileParams, ToolName } from "./generated/mcp-tools";

// Type-safe params
const params: ReadFileParams = {
  path: "/etc/hosts",
};

// Type-safe tool name
const toolName: ToolName = "read_file";

Multiple MCP Servers

Generate types for multiple servers with different prefixes:

# Apollo GraphQL MCP
mcp-to-typescript-codegen -c "apollo-mcp" -n Apollo -o "./types/apollo.ts"

# Filesystem MCP
mcp-to-typescript-codegen -c "npx" -a "@modelcontextprotocol/server-filesystem,/tmp" -n Filesystem -o "./types/filesystem.ts"

Then import each:

import { ApolloExecuteParams, ApolloToolName } from "./types/apollo";
import { FilesystemReadFileParams, FilesystemToolName } from "./types/filesystem";

License

MIT

About

Generate TypeScript types from MCP servers

Resources

License

Contributing

Stars

Watchers

Forks

Packages

No packages published