Getting Started
This guide shows how to installmcpfy-sdk, create an MCP server, register tools, run the server over stdio or HTTP, and connect to MCP servers using the mcpfy client.
mcpfy is a lightweight TypeScript SDK built on top of the official Model Context Protocol SDK. It provides higher-level APIs for common MCP operations while keeping access to the underlying official MCP implementation when needed.
Prerequisites
Before using mcpfy, make sure your development environment has:- Node.js
^20.19.0or>=22.12.0(Node.js 21.x is not supported) - npm, pnpm, or another Node.js package manager
- Basic TypeScript knowledge
- Basic understanding of MCP concepts such as servers, clients, tools, resources, and prompts
Installation
Install the SDK using your preferred package manager.npm
pnpm
yarn
Creating a TypeScript Project
A simple MCP server project can be structured as:package.json can look like:
Creating Your First MCP Server
The main server class isMCPServer.
Import it from:
MCPServer wraps the official MCP McpServer implementation and provides higher-level APIs for registering tools, prompts, resources, resource templates, and widgets.
Adding a Tool
Tools are executable functionality exposed by an MCP server. Register a tool with:ToolContext:
outputSchema when structured output should be constrained.
Starting the Server
mcpfy supports two transport modes:stdiohttp
stdio.
Therefore:
Using the stdio Transport
stdio is commonly used when an MCP host launches and manages the server process itself. Example:listen() result is:
Using the HTTP Transport
mcpfy can also expose an MCP server over HTTP. Use:Configuring the HTTP Port
The HTTP port can be configured directly:listen()portoption--portcommand-line argumentPORTenvironment variable3000
0 as the port to let the operating system choose a free port. The actual bound port is returned by listen().
Command-Line Port Configuration
mcpfy supports both:parsePortFromArgv() function reads these arguments:
--port arguments are present, the last one wins.
Using the PORT Environment Variable
The port can also be supplied through the environment.
On PowerShell:
listen() port or valid command-line port is provided.
Using a Custom MCP Path
The default MCP HTTP pathname is:basePath:
Using a Custom Host
The HTTP host can be configured using thehost option:
host option is only relevant to HTTP transport.
Suppressing the HTTP Startup Message
By default, mcpfy logs the local MCP URL when an HTTP server starts. This can be disabled using:Understanding the Listen Result
listen() returns a ListenResult.
For HTTP:
port, host, and url describe the bound HTTP server.
For stdio:
Complete HTTP Server Example
Server Metadata
When creating anMCPServer, the configuration is:
name
Required server name.
version
Required server version.
description
Optional server description.
basePath
Optional HTTP pathname for the MCP endpoint.
Default:
icon
Optional server icon advertised through MCP initialization.
Supported sources include:
- Remote URLs
data:URIs- Local file paths
file:URLs
auth
Optional HTTP authentication configuration.
jwksVerifier, oauthAuth0Provider, and oauthWorkOSProvider are available from the server package.
widgetsDir
Optional root directory for widget folders.
Default:
Accessing the Native MCP Server
mcpfy does not completely hide the official MCP SDK. The underlying official server instance is available through:McpServer.
This provides an escape hatch for advanced use cases that require direct access to the underlying MCP implementation.
Closing a Server
A server can be stopped using:close():
- Closes the HTTP server if it is running.
- Closes mounted remote connections.
- Clears the remote connection list.
- Closes the underlying native MCP server.
Refreshing MCP Data
mcpfy provides methods for notifying clients when registered MCP data changes.Refresh a resource
Refresh resources
Refresh tools
Refresh prompts
Using the MCP Client
mcpfy also provides a client abstraction for connecting to configured MCP servers. Import it from:mcpServers configuration:
Creating a Session
Create a session for a configured server:Creating All Sessions
To create sessions for all configured servers:Project Structure Recommendation
A small MCP project can use:Package Entry Points
mcpfy exposes functionality through separate package entry points.Main package
Server
MCPServerparsePortFromArgv- Server configuration and listen types
- Tool and prompt types
- Resource types
- Tool context types
- Widget types
- Authentication helpers
- Response helpers
- Server icon types
- Remote server configuration
Client
MCPClientMCPSessionBaseConnectorStdioConnectorHttpConnectorcreateConnectorFromConfig
React Widget
React widget functionality is exposed through:API Summary
MCPServer
Creates and manages an MCP server.
server.tool()
Registers an MCP tool.
server.prompt()
Registers an MCP prompt.
server.resource()
Registers a static MCP resource.
server.resourceTemplate()
Registers a dynamic MCP resource template.
server.listen()
Starts the MCP server.
server.close()
Stops the server and closes associated connections.
server.nativeServer
Provides direct access to the underlying official MCP server.
server.refreshResource()
Notifies subscribed clients that a resource has changed.
server.refreshResources()
Notifies clients that the resource list should be refreshed.
server.refreshTools()
Notifies clients that the tool list should be refreshed.
server.refreshPrompts()
Notifies clients that the prompt list should be refreshed.
parsePortFromArgv()
Reads a valid --port argument from command-line arguments.

