mcpfy Architecture
1. Overview
mcpfy is a lightweight TypeScript SDK built on top of the official Model Context Protocol SDK. Its architecture is designed to simplify the development of MCP servers and clients while preserving access to the underlying MCP implementation. The SDK provides abstractions for:- MCP servers
- MCP clients
- Tools
- Prompts
- Resources
- Resource templates
- Authentication
- HTTP and stdio transports
- Remote MCP server mounting
- Interactive widgets
- MCP-UI
- MCP Apps
- Apps SDK
- React-based widget runtimes
- Host communication and capabilities
- Resource subscriptions and refresh
- OAuth/JWT authentication
nativeServer whenever advanced functionality is required.
2. Package Structure
The main package is located at:3. Server Architecture
The central server abstraction is theMCPServer class.
It wraps the official:
4. MCPServer Initialization
MCPServer accepts an MCPServerConfig object.
The configuration includes:
- name
- version
- title
- icons
- description/instructions
- logging capability
- tool list-change notifications
- prompt list-change notifications
- resource subscriptions
- resource list-change notifications
5. Tool Registration Architecture
Tools are registered through:MCPServer focused on the public API while tool-specific logic remains inside the tools module.
A tool definition may contain information such as:
- name
- title
- description
- input schema
- output schema
- metadata
6. Tool Context
Tool execution can access contextual information throughToolContext.
The context is constructed using:
- logging
- authentication information
- requesting model sampling
- forwarding authentication headers
- sending requests to external services
- URL-related interactions
7. Prompt Architecture
Prompts are registered using:8. Resource Architecture
Resources are registered through:9. Refresh and Change Notifications
The SDK contains a dedicated:- resources
- tools
- prompts
10. Resource Subscriptions
Resource subscriptions are enabled through:11. Transport Architecture
The SDK supports two server transports:"stdio""http"
12. HTTP Path and Port Resolution
The HTTP MCP endpoint defaults to:13. Authentication Architecture
Authentication is implemented as a separate server subsystem. Relevant files include:- Token extraction
- Token verification
- Authentication information
- HTTP transport enforcement
14. Bearer Token Middleware
The file:15. JWT and JWKS Verification
The SDK provides a generic JWT verification implementation through:createRemoteJWKSet, which provides remote key retrieval and caching behavior.
The verifier also extracts the OAuth-style scope claim when available.
For example:
16. Authentication Provider Abstraction
The server authentication system uses theAuthConfig abstraction rather than coupling the server to a specific identity provider.
This allows authentication to be implemented using:
- custom header verification
- JWT/JWKS verification
- OAuth providers
- provider-specific presets
17. Client Architecture
The client implementation is located in:18. Client Session Management
MCPClient maintains sessions in:
- Checks whether a session already exists.
- Finds the server configuration.
- Creates the appropriate connector.
- Connects to the server.
- Creates an
MCPSession. - Stores the session.
- Returns the session.
19. Connector Architecture
Connectors are implemented in:BaseConnectorStdioConnectorHttpConnectorcreateConnectorFromConfig
MCPClient.
20. MCP Session Architecture
The session abstraction is implemented in:21. Remote MCP Server Mounting
mcpfy can expose functionality from other HTTP MCP servers through:22. Widget Architecture
Widgets are one of the major architectural components of mcpfy. The widget subsystem is located under:- MCP-UI
- MCP Apps
- Apps SDK
23. Widget Registration
Widgets can be registered through the server’s widget API. Internally:- name
- title
- description
- input schema
- content
- protocols
- CSP configuration
- size configuration
- callback
24. Widgets as MCP Tools
An important architectural decision is that widgets are connected to MCP tools. When a widget is registered, mcpfy registers an MCP tool with the widget’s name. The execution flow is:25. Widget Registry
Widget registry functionality is implemented in:MCPServer is created.
The default widget directory is:
26. Widget Preparation and Bundling
Widget preparation is handled by:27. Widget Security and CSP
Widget content can specify Content Security Policy settings. The implementation is located in:28. Client Widget Architecture
The client-side widget communication layer is located in:29. React Widget Runtime
The React-specific runtime is implemented in:- host connection
- protocol detection
- tool input
- tool output
- pending state
- errors
- theme
- locale
- platform
- layout mode
- widget state
- model context
- view tools
- external links
- follow-up messages
30. Host Runtime Architecture
The runtime creates an internal adapter around the detected host. Conceptually:31. Protocol Detection
The widget runtime uses:- apps-sdk
- mcp-apps
- mcp-ui
- none
32. Tool Communication from Widgets
The hook:33. Widget State
The runtime supports persistent widget state through:34. Model Context
The hook:35. View Tools
The runtime supports tools registered directly by a mounted view through:36. Host Capabilities
The widget runtime determines supported capabilities using:- tool calling
- display mode support
- model context
- view tools
- host-specific features
37. Theme Management
The React runtime supports:ThemeProvider stores the active theme and updates:
38. Layout Management
Widgets can interact with host display modes through:- current layout mode
- available layout modes
39. External Links and Follow-Up Messages
The widget runtime provides:HostAdapter.
For example:
40. Widget Size Reporting
The React runtime automatically reports widget dimensions to its parent. It calculates the document’s:- scrollHeight
- scrollWidth
ResizeObserver is used to detect changes.
The architecture is:
41. Shared Response Helpers
Common MCP response helpers are implemented in:- text
- markdown
- image
- object
- error
42. Authentication Client Support
Client-side authentication functionality is located under:NodeOAuthClientProviderOAuthSessionStoreFileKVStoreKVStore
43. Persistence Abstraction
TheKVStore abstraction provides a simple key-value persistence interface.
The file-based implementation:
44. CLI Architecture
The CLI entry point is:package.json.
The CLI can be used to work with MCP server execution and command-line configuration such as HTTP ports.
This keeps command-line concerns separate from the core SDK classes.
45. Public API Architecture
The package uses multiple public entry points. The main entry point is:46. Build Architecture
The package is written in TypeScript and uses:- tsup
- TypeScript
- Vite
- Vitest
- Bundles runtime JavaScript using tsup.
- Generates TypeScript declaration files using tsc.
47. Dependency Architecture
The SDK intentionally builds on established MCP and web technologies. Important dependencies include:@modelcontextprotocol/sdk@modelcontextprotocol/ext-apps@mcp-ui/serverjosereactreact-domvitetailwindcss

