MCP server template
A template for creating a Model Context Protocol server using FastMCP on Apify platform.
This template includes a simple example MCP server with:
An
addtool that adds two numbers together with structured outputA dummy
calculator-inforesource endpointPay Per Event monetization support
How to use
Modify the server: Edit
src/main.pyto add your own tools and resourcesAdd new tools: Use the
@server.tool()decorator to register new toolsAdd new resources: Use the
@server.resource()decorator to register new resourcesUpdate billing: Configure billing events in
.actor/pay_per_event.jsonand charge for tool calls
The server runs on port 3000 (or APIFY_CONTAINER_PORT if set) and exposes the MCP protocol at the /mcp endpoint.
Running locally
The server will start and listen for MCP requests at http://localhost:3000/mcp
Deploying to Apify
Push your Actor to the Apify platform and configure standby mode.
Then connect to the Actor endpoint with your MCP client: https://me--my-mcp-server.apify.actor/mcp using the Streamable HTTP transport.
Important: When connecting to your deployed MCP server, pass your Apify API token in the Authorization header as a Bearer token:
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-call event:
In the Actor, trigger the event with:
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 eventand add the schema. An example schema can be found in pay_per_event.json.
Resources
Getting started
For complete information see this article. To run the Actor use the following command:
Deploy to Apify
Connect Git repository to Apify
If you've created a Git repository for the project, you can easily connect to Apify:
Go to Actor creation page
Click on Link Git Repository button
Push project on your local machine to Apify
You can also deploy the project on your local machine to Apify without the need for the Git repository.
Log in to Apify. You will need to provide your Apify API Token to complete this action.
apify loginDeploy your Actor. This command will deploy and build the Actor on the Apify Platform. You can find your newly created Actor under Actors -> My Actors.
apify push
Documentation reference
To learn more about Apify and Actors, take a look at the following resources: