MCP Server
mcpfy-sdk/server provides the server-side API for building MCP servers with tools, prompts, resources, resource templates, widgets, authentication, and multiple transports.
The main entry point is MCPServer.
1. Importing the Server API
2. Creating an MCP Server
A server requires a name and version.MCPServer class wraps the official MCP SDK server while providing a simpler declarative API.
The underlying native server remains accessible through:
3. Server Configuration
The server accepts anMCPServerConfig object.
Configuration options
Example:
4. Server Capabilities
WhenMCPServer is created, mcpfy initializes the underlying official MCP server with support for:
- Logging
- Tools
- Prompts
- Resources
- Resource subscriptions
- List-change notifications
5. Registering Tools
Tools are registered using:tools.md.
6. Registering Prompts
Prompts are registered using:prompts.md.
7. Registering Resources
Resources are registered using:resources.md.
8. Registering Widgets
mcpfy supports interactive MCP widgets. Widgets can be registered through:widget() method is deprecated for new widget implementations.
The recommended approach is to specify a widget folder through a tool definition:
widgets.md and widget-react.md.
9. Starting the Server
The server is started using:10. HTTP Transport
An MCP server can also be started using HTTP:11. HTTP Configuration
ListenOptions controls how the server starts.
transport
Selects the server transport.
port
Specifies the HTTP port.
0 allows the operating system to select an available port:
listen().
host
Specifies the HTTP listening host.
silent
Controls whether the HTTP startup URL is printed.
12. HTTP Port Resolution
When using HTTP transport, mcpfy determines the port using the following priority:--port=N form is supported as well:
13. Parsing a Port from Arguments
The helper:mcpfy-sdk/server.
Example:
14. Environment Port
When no explicit port or command-line port is provided, mcpfy checks:8080.
If the environment variable is missing or invalid, mcpfy falls back to port 3000.
15. Server Listen Result
listen() returns a ListenResult.
16. Custom MCP HTTP Path
The HTTP MCP endpoint defaults to:basePath.
17. Server Icons
An MCP server can advertise an icon through theicon configuration.
- A remote URL
- A
data:URI - A local file path
- A
file:URL - A
ServerIconobject
data: URIs so MCP clients can display them.
18. Authentication
HTTP servers can require authentication using theauth configuration.
authentication.md.
19. Accessing the Native MCP Server
The underlying official MCP server is exposed through:20. Refreshing Resources
mcpfy provides resource refresh functionality. To notify subscribed clients that a specific resource has changed:21. Refreshing the Resource List
To tell clients to request the resource list again:22. Refreshing the Tool List
To tell clients to request the tool list again:23. Refreshing the Prompt List
To tell clients to request the prompt list again:24. Mounting Remote MCP Servers
mcpfy can expose tools, prompts, and resources from other HTTP MCP servers through the current server. This is done using:25. Closing the Server
A running server can be closed using:close() performs cleanup for:
- The HTTP server, if running.
- Mounted remote server connections.
- The underlying native MCP server.
26. HTTP Server Handle
After starting an HTTP server, additional information is available through:HttpHandle while the HTTP server is running.
It becomes undefined after the server is closed.
27. Complete HTTP Server Example
28. Complete Stdio Server Example
29. Recommended Server Structure
A larger MCP application can organize the server separately from its tools and other features. Example:30. Public Server API
Themcpfy-sdk/server entry point exports the following major APIs.
Server
Server types
Tools
Prompts
Resources
Context
Responses
Widgets
Authentication
Remote servers
31. Server API Summary
32. Recommended Usage Pattern
A typical mcpfy server follows this pattern:MCPServer abstraction supports both approaches, allowing the application logic to remain independent of the selected transport.
