API Reference
This document provides a complete reference for themcp-use/server API, including all classes, methods, and type definitions.
Server Creation
createMCPServer()
Creates a new MCP server instance with Express integration.
name(string) - Unique identifier for your serverconfig(Partial<ServerConfig>) - Optional configuration object
McpServerInstance - Server instance with both MCP and Express methods
Example:
Server Configuration
ServerConfig
Configuration object for server initialization.
allowedOrigins Configuration:
The allowedOrigins option controls DNS rebinding protection behavior:
-
Development mode (NODE_ENV !== “production”):
- If not set: All origins are allowed (DNS rebinding protection disabled)
- This enables direct browser connections from any origin for easier development
-
Production mode (NODE_ENV === “production”):
- If not set: DNS rebinding protection is disabled (not recommended for production)
- If set to empty array: DNS rebinding protection is disabled
- If set with origins: DNS rebinding protection is enabled with those specific origins
autoCreateSessionOnInvalidId Configuration:
The autoCreateSessionOnInvalidId option controls behavior when a client sends a request with an invalid or expired session ID:
-
Default:
true(compatible with ChatGPT and other non-compliant clients)- Server automatically creates a new session when invalid session ID is detected
- Allows seamless reconnection after server restarts
- Recommended for most use cases, especially when using ChatGPT or other OpenAI clients
-
When set to
false:- Server returns HTTP 404 Not Found for invalid session IDs (MCP protocol spec compliant)
- Clients must explicitly send a new
initializerequest - Use only if you’re certain all clients properly handle 404 errors by reinitializing
InitializeRequest when they receive HTTP 404 in response to a request containing an MCP-Session-Id. However, some clients (like ChatGPT) don’t properly implement this behavior.
Example:
Core Server Methods
server.tool()
Registers a tool that clients can invoke.
definition(ToolDefinition) - Tool configuration and handler
server.resource()
Registers a static resource that clients can read.
definition(ResourceDefinition) - Resource configuration and content
server.resourceTemplate()
Registers a dynamic resource template with parameters.
definition(ResourceTemplateDefinition) - Template configuration
server.prompt()
Registers a prompt template for AI models.
definition(PromptDefinition) - Prompt configuration and generator
server.uiResource()
Registers a UI widget as both a tool and resource.
definition(UIResourceDefinition) - UI widget configuration
server.listen()
Starts the HTTP server with MCP endpoints.
port(number, optional) - Port to listen on (default: 3000)
Type Definitions
ToolDefinition
Configuration for tools.
ResourceDefinition
Configuration for static resources.
ResourceTemplateDefinition
Configuration for dynamic resource templates.
PromptDefinition
Configuration for prompt templates.
UIResourceDefinition
Configuration for UI widgets.
InputDefinition
Parameter definition for tools and prompts.
ResourceAnnotations
Metadata annotations for resources.
WidgetProps
Property definitions for UI widgets.
AppsSdkMetadata
OpenAI Apps SDK metadata for widgets.
Express Integration
The server instance proxies all Express methods, allowing you to use Express functionality directly:Common Express Methods
Example Express Usage
Return Types
CallToolResult
Tool execution result.
ReadResourceResult
Resource read result.
PromptResult
Prompt generation result.
Utility Functions
UI Resource Helpers
The framework provides utility functions for creating UI resources:Error Handling
The framework handles errors gracefully:Best Practices
- Use TypeScript - Leverage type safety for better development experience
- Handle Errors - Always handle errors gracefully in callbacks
- Validate Input - Validate parameters in tool callbacks
- Use Annotations - Provide metadata for better client integration
- Chain Methods - Use fluent API for cleaner code
- Document Tools - Provide clear descriptions for all tools and resources
React Components & Hooks
Themcp-use/react package provides React components and hooks for building OpenAI Apps SDK widgets.
Components
McpUseProvider
Unified provider component that combines all common React setup.
WidgetControls
Wrapper component that adds control buttons for widget debugging and view controls.
ErrorBoundary
Error boundary component for graceful error handling.
Image
Image component that handles both data URLs and public file paths.
ThemeProvider
Theme provider for consistent theme management.
Hooks
useWidget<TProps, TOutput, TMetadata, TState>
Main hook providing type-safe access to all widget capabilities.
useWidgetProps<TProps>
Get only the widget props.
useWidgetTheme()
Get only the theme value.
useWidgetState<TState>
Get state management (state and setState).
Type Exports
Next Steps
- Tools Guide - Advanced tool patterns
- Resources Guide - Resource management
- UI Widgets - Building interactive widgets
- Widget Components - React components reference
- Examples - Real-world implementations