Skip to main content

Getting Started

This guide shows how to install mcpfy-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.0 or >=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
The package declares the following Node.js engine requirement:

Installation

Install the SDK using your preferred package manager.

npm

pnpm

yarn

mcpfy uses ES modules, so projects consuming the package should use an ESM-compatible configuration. For example:

Creating a TypeScript Project

A simple MCP server project can be structured as:
A minimal package.json can look like:

Creating Your First MCP Server

The main server class is MCPServer. Import it from:
Create a server by providing a name and version:
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:
For example:
The tool definition can include a Zod input schema:
A tool callback receives the tool input and the mcpfy ToolContext:
The callback returns a tool result or a supported mcpfy content result. Tools can also define an outputSchema when structured output should be constrained.

Starting the Server

mcpfy supports two transport modes:
  • stdio
  • http
The default transport is stdio. Therefore:
is equivalent to:

Using the stdio Transport

stdio is commonly used when an MCP host launches and manages the server process itself. Example:
The server communicates through standard input and standard output. The stdio listen() result is:

Using the HTTP Transport

mcpfy can also expose an MCP server over HTTP. Use:
Example:
By default, the HTTP server uses:
Therefore, the default endpoint is:

Configuring the HTTP Port

The HTTP port can be configured directly:
The server will then be available at:
mcpfy resolves the HTTP port using this priority:
  1. listen() port option
  2. --port command-line argument
  3. PORT environment variable
  4. 3000
For example:
takes priority over:
and:
You can also pass 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:
and:
For example:
The exported parsePortFromArgv() function reads these arguments:
If multiple valid --port arguments are present, the last one wins.

Using the PORT Environment Variable

The port can also be supplied through the environment. On PowerShell:
On Unix-like shells:
The environment variable is used when no explicit listen() port or valid command-line port is provided.

Using a Custom MCP Path

The default MCP HTTP pathname is:
A custom path can be configured through basePath:
The MCP endpoint will then be:

Using a Custom Host

The HTTP host can be configured using the host option:
The default host is:
The 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:
The result contains:
The HTTP port, host, and url describe the bound HTTP server. For stdio:
the result is:

Complete HTTP Server Example


Server Metadata

When creating an MCPServer, the configuration is:

name

Required server name.

version

Required server version.

description

Optional server description.
The description is passed to the underlying MCP server as its instructions.

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
For example:
Local icons are converted to data URIs before being advertised.

auth

Optional HTTP authentication configuration.
Authentication applies to HTTP transport. Authentication helpers such as jwksVerifier, oauthAuth0Provider, and oauthWorkOSProvider are available from the server package.

widgetsDir

Optional root directory for widget folders. Default:
This directory is used when tools reference widgets by folder name.

Accessing the Native MCP Server

mcpfy does not completely hide the official MCP SDK. The underlying official server instance is available through:
For example:
Its type is based on the official MCP SDK’s 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:
Example:
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

This notifies subscribed clients that the resource has new content.

Refresh resources

This tells clients to refresh their resource list.

Refresh tools

This tells clients to refresh their tool list.

Refresh prompts

This tells clients to refresh their prompt list. Resource subscriptions and resource-list-change notifications are enabled by the server runtime.

Using the MCP Client

mcpfy also provides a client abstraction for connecting to configured MCP servers. Import it from:
Create a client with an mcpServers configuration:
The client maintains MCP sessions for configured servers.

Creating a Session

Create a session for a configured server:
If a session for that server already exists, mcpfy reuses it rather than creating another connection.

Creating All Sessions

To create sessions for all configured servers:
The result is keyed by the configured server names:

Project Structure Recommendation

A small MCP project can use:
For a larger project:
This structure is not required by the SDK, but provides a clean way to organize larger MCP applications.

Package Entry Points

mcpfy exposes functionality through separate package entry points.

Main package

Server

The server entry point includes:
  • MCPServer
  • parsePortFromArgv
  • 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

The client entry point provides:
  • MCPClient
  • MCPSession
  • BaseConnector
  • StdioConnector
  • HttpConnector
  • createConnectorFromConfig

React Widget

React widget functionality is exposed through:
See the widget documentation for the complete React widget API.

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.
or:

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.
The last valid port argument wins.

Best Practices

Use MCPServer for common server operations

Use the higher-level mcpfy APIs rather than manually registering every operation against the native MCP server.

Use schemas for tool inputs

When a tool expects structured input, define its input schema explicitly:
This makes the expected tool input clear and allows the underlying MCP registration to use the schema.

Use HTTP and stdio according to the deployment environment

Use stdio when an MCP host launches the server as a child process. Use HTTP when the MCP server needs to be exposed as an HTTP endpoint.

Keep server metadata meaningful

Use descriptive names, versions, descriptions, and paths:

Use the native server only when necessary

For common operations, prefer mcpfy’s higher-level APIs. Use:
when direct access to functionality from the official MCP SDK is required.

Detect optional widget capabilities

Widgets should not assume every MCP host supports every optional feature. Use the widget runtime’s capability information before relying on host-specific functionality.

Summary

mcpfy provides a lightweight API for building MCP applications while remaining compatible with the official MCP SDK. The main server workflow is:
For HTTP:
For clients:
mcpfy provides higher-level APIs for servers, clients, tools, prompts, resources, resource templates, authentication, and widgets while retaining access to the underlying official MCP implementation when advanced control is required.