Back to template gallery

TypeScript MCP server

Example of how to turn a Model Context Protocol stdio server into an Apify Actor.

Language

typescript

Tools

mcp

Features

src/main.ts

src/billing.ts

1/**
2 * MCP Server - Main Entry Point
3 *
4 * This file serves as the entry point for the MCP Server Actor.
5 * It sets up a proxy server that forwards requests to the locally running
6 * MCP server, which provides a Model Context Protocol (MCP) interface.
7 */
8
9// Apify SDK - toolkit for building Apify Actors (Read more at https://docs.apify.com/sdk/js/)
10import { Actor, log } from 'apify';
11
12import { startServer } from './server.js';
13
14// This is an ESM project, and as such, it requires you to specify extensions in your relative imports
15// Read more about this here: https://nodejs.org/docs/latest-v18.x/api/esm.html#mandatory-file-extensions
16// Note that we need to use `.js` even when inside TS files
17// import { router } from './routes.js';
18
19// Configuration constants for the MCP server
20// Command to run the Everything MCP Server
21// TODO: Do not forget to install the MCP server in package.json (using `npm install ...`)
22const MCP_COMMAND = [
23 'npx',
24 '@modelcontextprotocol/server-everything',
25];
26
27// Check if the Actor is running in standby mode
28const STANDBY_MODE = process.env.APIFY_META_ORIGIN === 'STANDBY';
29const SERVER_PORT = parseInt(process.env.ACTOR_WEB_SERVER_PORT || '', 10);
30
31// Initialize the Apify Actor environment
32// The init() call configures the Actor for its environment. It's recommended to start every Actor with an init()
33await Actor.init();
34
35// Charge for Actor start
36await Actor.charge({ eventName: 'actor-start' });
37
38if (!STANDBY_MODE) {
39 // If the Actor is not in standby mode, we should not run the MCP server
40 const msg = 'This Actor is not meant to be run directly. It should be run in standby mode.';
41 log.error(msg);
42 await Actor.exit({ statusMessage: msg });
43}
44
45await startServer({
46 serverPort: SERVER_PORT,
47 command: MCP_COMMAND,
48});

MCP server template

A template for running and monetizing a Model Context Protocol server using stdio transport on Apify platform. This allows you to run any stdio MCP server as a standby Actor and connect via either the streamable HTTP transport or the legacy SSE transport with an MCP client.

How to use

Change the MCP_COMMAND to spawn your stdio MCP server in src/main.ts, and don't forget to install the required MCP server in the package.json (using npm install ...). By default, this template runs an Everything MCP Server using the following command:

const MCP_COMMAND = [
'npx',
'@modelcontextprotocol/server-everything',
];

Alternatively, you can use the mcp-remote tool to turn a remote MCP server into an Actor. For example, to connect to a remote server over SSE with authentication:

const MCP_COMMAND = [
'npx',
'mcp-remote',
'https://mcp.apify.com/sse',
'--transport',
'sse-only',
'--header',
'Authorization: Bearer TOKEN',
];

Feel free to configure billing logic in .actor/pay_per_event.json and src/billing.ts.

Push your Actor to the Apify platform, configure standby mode, and then connect to the Actor standby URL with your MCP client. You can use either:

Important: When connecting to your deployed MCP server, you must pass your Apify API token in the Authorization header as a Bearer token. For example:

Authorization: Bearer <YOUR_APIFY_API_TOKEN>

This is required for authentication and to access your Actor endpoint.

Pay per event

This template uses the Pay Per Event (PPE) monetization model, which provides flexible pricing based on defined events.

To charge users, define events in JSON format and save them on the Apify platform. Here is an example schema with the tool-request event:

[
{
"tool-request": {
"eventTitle": "Price for completing a tool request",
"eventDescription": "Flat fee for completing a tool request.",
"eventPriceUsd": 0.05
}
}
]

In the Actor, trigger the event with:

await Actor.charge({ eventName: 'tool-request' });

This approach allows you to programmatically charge users directly from your Actor, covering the costs of execution and related services.

To set up the PPE model for this Actor:

  • Configure Pay Per Event: establish the Pay Per Event pricing schema in the Actor's Monetization settings. First, set the Pricing model to Pay per event and add the schema. An example schema can be found in .actor/pay_per_event.json.

Resources

Already have a solution in mind?

Sign up for a free Apify account and deploy your code to the platform in just a few minutes! If you want a head start without coding it yourself, browse our Store of existing solutions.