v0-mcp-server
Version:
Simplified MCP Server for v0.dev with auto-discovery and direct SDK dispatch
347 lines (334 loc) • 10.5 kB
TypeScript
/**
* Core Types for Simplified v0 MCP Server
*/
interface SimplifiedConfig {
name?: string;
version?: string;
apiKey: string;
verbose?: boolean;
timeout?: number;
}
type Either<L, R> = {
_tag: 'Left';
left: L;
} | {
_tag: 'Right';
right: R;
};
declare enum SdkNamespace {
CHATS = "chats",
PROJECTS = "projects",
DEPLOYMENTS = "deployments",
HOOKS = "hooks",
INTEGRATIONS = "integrations",
RATE_LIMITS = "rateLimits",
USER = "user"
}
interface DiscoveredMethod {
namespace: string;
method: string;
toolName: string;
sdkFunction: Function;
parameters: MethodParameter[];
returnType: string;
fullPath: string;
}
interface MethodParameter {
name: string;
type: string;
required: boolean;
description?: string;
defaultValue?: any;
}
interface ToolMapping {
discoveredTools: Map<string, DiscoveredMethod>;
aliases: Map<string, ToolAlias>;
schemas: Map<string, ToolSchema>;
}
interface ToolSchema {
name: string;
description: string;
inputSchema: {
type: 'object';
properties: Record<string, ParameterSchema>;
required: string[];
additionalProperties: boolean;
};
}
interface ParameterSchema {
type: 'string' | 'number' | 'boolean' | 'object' | 'array';
description: string;
minimum?: number;
maximum?: number;
minLength?: number;
maxLength?: number;
enum?: string[];
default?: any;
}
interface ToolExecutionContext {
toolName: string;
parameters: any;
timestamp: Date;
}
interface ToolExecutionResult {
success: boolean;
result?: any;
formattedResult?: any;
error?: string;
errorCode?: string;
duration: number;
}
interface ToolAlias {
legacyName: string;
newName: string;
transformParams: (params: any) => any;
transformResponse: (response: any) => any;
}
interface DiscoveryResult {
methods: DiscoveredMethod[];
totalDiscovered: number;
namespaces: string[];
errors: string[];
}
interface V0ClientConfig {
apiKey: string;
}
declare const SETUP_URL = "https://v0.dev/chat/settings/keys";
declare const PERFORMANCE_LIMITS: {
MAX_DESCRIPTION_LENGTH: number;
MAX_CHANGES_LENGTH: number;
MAX_CHAT_ID_LENGTH: number;
REQUEST_TIMEOUT: number;
MAX_CONCURRENT_REQUESTS: number;
};
declare class SimplifiedMcpServer {
private server;
private config;
private v0Client;
private toolMapping;
private isStarted;
constructor(config?: Partial<SimplifiedConfig>);
start(): Promise<void>;
private createSimplifiedConfig;
private createV0Client;
private discoverAndRegisterTools;
private registerDiscoveredTool;
/**
* Registers minimal toolset as fallback when SDK discovery fails
*/
private registerMinimalToolset;
/**
* Creates core tool definitions when SDK discovery fails
*/
private createCoreToolDefinitions;
/**
* Registers MCP request handlers
*/
private registerRequestHandlers;
/**
* Handles tool call execution with direct SDK dispatch
*/
handleToolCall(toolName: string, params: any): Promise<ToolExecutionResult>;
/**
* Handles legacy tool calls with parameter/response transformation
*/
private handleLegacyToolCall;
/**
* Formats tool execution result for MCP response
*/
private formatToolResponse;
/**
* Gets description for legacy tools
*/
private getLegacyToolDescription;
/**
* Gets input schema for legacy tools
*/
private getLegacyToolInputSchema;
/**
* Checks if the server is running
*/
isRunning(): boolean;
/**
* Gets server configuration
*/
getConfig(): SimplifiedConfig;
/**
* Gets discovered tools information
*/
getDiscoveredTools(): DiscoveredMethod[];
}
declare function introspectSdkClient(client: any): DiscoveryResult;
declare function generateToolName(namespace: string, method: string): string;
declare function extractMethodParameters(fn: Function): MethodParameter[];
/**
* Tool Schema Generation Utilities
*
* Automatically generates MCP-compliant tool schemas from discovered SDK methods.
* Handles parameter mapping and type conversion between SDK and MCP formats.
*/
/**
* Generates MCP tool schema from discovered method
*
* @param method - Discovered SDK method
* @returns MCP-compliant tool schema
*/
declare function generateMcpToolSchema(method: DiscoveredMethod): ToolSchema;
/**
* Maps SDK parameters to MCP format with proper transformations
*
* @param sdkParams - Original SDK parameters
* @param method - Discovered method for context
* @returns MCP-compatible parameters
*/
declare function mapSdkParamsToMcp(sdkParams: any, method: DiscoveredMethod): any;
/**
* Infers parameter types from method context and parameter names
*
* @param method - Discovered method
* @param paramName - Parameter name
* @returns Inferred parameter type with constraints
*/
declare function inferParameterTypes(method: DiscoveredMethod): MethodParameter[];
/**
* Tool Alias Management
*
* Provides backward compatibility by mapping legacy tool names to new SDK-based tools.
* Handles parameter and response transformations to maintain identical behavior.
*/
/**
* Creates the complete alias mapping for backward compatibility
*
* @returns Map of legacy tool names to their new SDK equivalents
*/
declare function createAliasMap(): Map<string, ToolAlias>;
/**
* Transforms legacy tool parameters to new SDK format
*
* @param toolName - Legacy tool name
* @param params - Legacy parameters
* @returns Transformed parameters for SDK method
*/
declare function transformLegacyParams(toolName: string, params: any): any;
/**
* Transforms SDK response back to legacy format
*
* @param toolName - Legacy tool name
* @param response - SDK response
* @returns Transformed response in legacy format
*/
declare function resolveLegacyResponse(toolName: string, response: any): any;
/**
* Resolves legacy tool name to new SDK tool name
*
* @param toolName - Tool name (legacy or new)
* @returns Resolved SDK tool name
*/
declare function resolveLegacyToolName(toolName: string): string;
/**
* Checks if a tool name is a legacy alias
*
* @param toolName - Tool name to check
* @returns True if toolName is a legacy alias
*/
declare function isLegacyTool(toolName: string): boolean;
/**
* Gets all legacy tool names for tool listing
*
* @returns Array of legacy tool names
*/
declare function getLegacyToolNames(): string[];
declare const left: <L, R>(value: L) => Either<L, R>;
declare const right: <L, R>(value: R) => Either<L, R>;
declare function validateToolParameters(toolName: string, params: any, schema: ToolSchema): Either<Error, any>;
declare function validateComponentDescription(description: string): Either<Error, string>;
declare function validateChatId(chatId: string): Either<Error, string>;
/**
* Response Formatting Utilities
*
* Formats SDK responses for consistent MCP output and handles
* backward compatibility response transformations.
*/
/**
* Formats SDK response for consistent MCP output
*
* @param response - Raw SDK response
* @param toolName - Name of the tool that generated the response
* @returns Formatted response for MCP
*/
declare function formatSdkResponse(response: any, toolName: string): any;
/**
* Formats error responses with consistent structure
*/
declare function formatErrorResponse(error: any, toolName: string, context?: string): any;
/**
* Formats execution result for MCP response text
*/
declare function formatExecutionResult(result: ToolExecutionResult): string;
/**
* API Key Management for v0 MCP Server
*
* Handles v0.dev API key validation, format checking, and secure storage
* following security requirements SR-1 and user story US-3.
*/
interface McpError {
code: string;
message: string;
data?: any;
}
/**
* Manages v0.dev API key validation and secure handling
*/
declare class ApiKeyManager {
private readonly apiKey;
/**
* Creates a new ApiKeyManager instance
* @param apiKey - API key from CLI argument or environment variable
*/
constructor(apiKey?: string);
/**
* Validates the API key format and availability
* @throws {Error} If API key is missing or invalid format
*/
private validate;
/**
* Validates API key format without exposing the actual key
* @param key - API key to validate
* @returns true if format appears valid
*/
private isValidFormat;
/**
* Returns the API key for use in v0.dev API calls
* @returns The validated API key
*/
getKey(): string;
/**
* Returns a redacted version for logging/debugging
* Never exposes the actual API key value
* @returns Redacted key representation
*/
toString(): string;
/**
* Creates a safe representation for JSON serialization
* Ensures API key is never accidentally logged or serialized
* @returns Safe object without sensitive data
*/
toJSON(): object;
/**
* Validates API key format statically without creating an instance
* @param key - API key to validate
* @returns Result indicating validation success or error
*/
static validateKeyFormat(key: string): {
valid: boolean;
error?: string;
};
/**
* Creates an MCP-formatted error for API key issues
* @param message - Error message
* @returns Standardized MCP error
*/
static createApiKeyError(message: string): McpError;
}
declare function startServer(apiKey?: string, verbose?: boolean): Promise<SimplifiedMcpServer>;
export { ApiKeyManager, type DiscoveredMethod, type DiscoveryResult, type Either, type MethodParameter, PERFORMANCE_LIMITS, type ParameterSchema, SETUP_URL, SdkNamespace, type SimplifiedConfig, SimplifiedMcpServer, type ToolAlias, type ToolExecutionContext, type ToolExecutionResult, type ToolMapping, type ToolSchema, type V0ClientConfig, createAliasMap, extractMethodParameters, formatErrorResponse, formatExecutionResult, formatSdkResponse, generateMcpToolSchema, generateToolName, getLegacyToolNames, inferParameterTypes, introspectSdkClient, isLegacyTool, left, mapSdkParamsToMcp, resolveLegacyResponse, resolveLegacyToolName, right, startServer, transformLegacyParams, validateChatId, validateComponentDescription, validateToolParameters };