|
| 1 | +--- |
| 2 | +id: mcp-servers |
| 3 | +title: Deploying MCP servers |
| 4 | +description: Deploy a Python MCP server as an Apify Actor and make its tools available to any MCP client. |
| 5 | +--- |
| 6 | + |
| 7 | +import RunnableCodeBlock from '@site/src/components/RunnableCodeBlock'; |
| 8 | + |
| 9 | +import McpServerExample from '!!raw-loader!roa-loader!./code/13_mcp_server.py'; |
| 10 | +import McpProxyExample from '!!raw-loader!roa-loader!./code/13_mcp_proxy.py'; |
| 11 | + |
| 12 | +In this guide, you'll learn how to deploy a [Model Context Protocol](https://modelcontextprotocol.io) (MCP) server as an Apify Actor. |
| 13 | + |
| 14 | +## Introduction |
| 15 | + |
| 16 | +The [Model Context Protocol](https://modelcontextprotocol.io) is an open standard that lets AI applications connect to external tools and data. An MCP server exposes tools, resources, and prompts, and any MCP client (such as Claude or an IDE assistant) can call them. Hosting that server as an Apify Actor turns it into a remote, always-ready service. |
| 17 | + |
| 18 | +Apify Actors are a good fit for MCP servers: |
| 19 | + |
| 20 | +- With [Actor Standby](https://docs.apify.com/platform/actors/development/programming-interface/standby), the platform keeps the Actor running in the background and routes incoming HTTP requests to it, so your server is always ready to answer an MCP client. |
| 21 | +- The platform scales instances with demand, keeps logs, and handles the network, so you don't operate any infrastructure. |
| 22 | +- Every request carries an Apify API token, so the platform authenticates clients for you. |
| 23 | +- Pay-per-event charging lets you monetize the server, for example per tool call. |
| 24 | + |
| 25 | +## Apify templates |
| 26 | + |
| 27 | +Apify provides two templates for building MCP servers: |
| 28 | + |
| 29 | +- Write a server from scratch with [FastMCP](https://gofastmcp.com/), starting from the [`python-mcp-empty`](https://apify.com/templates/python-mcp-empty) template. For details, see [MCP server](#mcp-server). |
| 30 | +- Wrap an existing MCP server (stdio, HTTP, or SSE) with a proxy, starting from the [`python-mcp-proxy`](https://apify.com/templates/python-mcp-proxy) template. For details, see [MCP proxy](#mcp-proxy). |
| 31 | + |
| 32 | +Both templates live in the [actor-templates repository](https://github.com/apify/actor-templates). To create a new project, use the [Apify CLI](https://docs.apify.com/cli), for example: |
| 33 | + |
| 34 | +```bash |
| 35 | +apify create my-mcp-server --template python-mcp-empty |
| 36 | +``` |
| 37 | + |
| 38 | +## Before you start |
| 39 | + |
| 40 | +To follow along, install the [Apify CLI](https://docs.apify.com/cli/docs/installation) and log in with `apify login`. Both examples build on [FastMCP](https://gofastmcp.com/) and are served by [uvicorn](https://www.uvicorn.org/), so declare them in your Actor's `requirements.txt` next to the SDK: |
| 41 | + |
| 42 | +```text |
| 43 | +apify |
| 44 | +fastmcp |
| 45 | +uvicorn |
| 46 | +``` |
| 47 | + |
| 48 | +## MCP server |
| 49 | + |
| 50 | +Build your own server when you want to expose your own tools and resources. The following Actor runs a small [FastMCP](https://gofastmcp.com/) server that exposes a single `add` tool and an informational resource. It serves the MCP protocol over the [Streamable HTTP](https://modelcontextprotocol.io/specification/2025-06-18/basic/transports#streamable-http) transport on the Actor's web server port: |
| 51 | + |
| 52 | +<RunnableCodeBlock className="language-python" language="python"> |
| 53 | + {McpServerExample} |
| 54 | +</RunnableCodeBlock> |
| 55 | + |
| 56 | +Note that: |
| 57 | + |
| 58 | +- A `build_server` helper registers the tools and resources. Add your own with the `@server.tool()` and `@server.resource()` decorators. |
| 59 | +- `server.http_app(transport='streamable-http')` returns an ASGI app that speaks MCP over Streamable HTTP, which [uvicorn](https://www.uvicorn.org/) then serves. |
| 60 | +- The server binds to `Actor.configuration.web_server_port` and `0.0.0.0`, so the platform can route the Actor's container URL to it. |
| 61 | +- The example serves in the background for a short window so the run finishes on its own. A production Actor keeps serving until the platform shuts it down. With [Standby](#exposing-it-over-standby), shutdown happens automatically once an instance has been idle for a while. |
| 62 | + |
| 63 | +## MCP proxy |
| 64 | + |
| 65 | +If you already have an MCP server, or want to expose a third-party one, you don't need to rewrite it. FastMCP can wrap an existing server and re-expose it over Streamable HTTP, so you put the same always-ready Apify endpoint in front of a server you didn't write. The following Actor proxies a remote MCP server, serving it on its web server port: |
| 66 | + |
| 67 | +<RunnableCodeBlock className="language-python" language="python"> |
| 68 | + {McpProxyExample} |
| 69 | +</RunnableCodeBlock> |
| 70 | + |
| 71 | +Note that: |
| 72 | + |
| 73 | +- `create_proxy` connects to the upstream server and returns a FastMCP instance, so the rest of the Actor is identical to a server you build yourself. |
| 74 | +- The example wraps a remote URL. To spawn and wrap a local stdio server, pass an `mcpServers` config instead: |
| 75 | + |
| 76 | + ```python |
| 77 | + proxy = create_proxy( |
| 78 | + {'mcpServers': {'arxiv': {'command': 'uv', 'args': ['run', 'arxiv-mcp-server']}}}, |
| 79 | + name='my-mcp-proxy', |
| 80 | + ) |
| 81 | + ``` |
| 82 | + |
| 83 | +- Serving works the same way as the server. You expose it over [Standby](#exposing-it-over-standby) the same way, so clients connect to `<actor-url>/mcp` with a bearer token. |
| 84 | +- To control which tools clients may call, or to charge per call, add a FastMCP [middleware](https://gofastmcp.com/servers/middleware) that hooks `on_list_tools` and `on_call_tool`. |
| 85 | + |
| 86 | +For a gateway with a tool whitelist and per-operation charging, start from the [`python-mcp-proxy`](https://apify.com/templates/python-mcp-proxy) template. |
| 87 | + |
| 88 | +## Exposing it over Standby |
| 89 | + |
| 90 | +Both the server and the proxy listen on the web server port, so you expose either one the same way. To make it an always-ready HTTP API, enable [Actor Standby](https://docs.apify.com/platform/actors/development/programming-interface/standby) and tell the platform where the MCP endpoint lives. Set both in the Actor's `.actor/actor.json`: |
| 91 | + |
| 92 | +```json |
| 93 | +{ |
| 94 | + "actorSpecification": 1, |
| 95 | + "name": "my-mcp-server", |
| 96 | + "usesStandbyMode": true, |
| 97 | + "webServerMcpPath": "/mcp" |
| 98 | +} |
| 99 | +``` |
| 100 | + |
| 101 | +Deploy the Actor with `apify push`. Once it's running, an MCP client connects to the Actor's URL using the Streamable HTTP transport, passing an [Apify API token](https://console.apify.com/account/integrations) as a bearer token: |
| 102 | + |
| 103 | +```json |
| 104 | +{ |
| 105 | + "mcpServers": { |
| 106 | + "my-mcp-server": { |
| 107 | + "url": "https://me--my-mcp-server.apify.actor/mcp", |
| 108 | + "headers": { |
| 109 | + "Authorization": "Bearer <YOUR_APIFY_API_TOKEN>" |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | +} |
| 114 | +``` |
| 115 | + |
| 116 | +## Monetizing with pay-per-event |
| 117 | + |
| 118 | +Both approaches support [pay-per-event charging](../concepts/pay-per-event), so you can cover the cost of running the server or turn it into a paid product. |
| 119 | + |
| 120 | +1. Define the events in the Actor's `.actor/pay_per_event.json`: |
| 121 | + |
| 122 | + ```json |
| 123 | + { |
| 124 | + "tool-call": { |
| 125 | + "eventTitle": "Price for completing a tool call", |
| 126 | + "eventDescription": "Flat fee for completing a tool call.", |
| 127 | + "eventPriceUsd": 0.05 |
| 128 | + } |
| 129 | + } |
| 130 | + ``` |
| 131 | + |
| 132 | +2. Charge the event from your code when a client calls a tool. The `add` tool from the [server example](#mcp-server) becomes `async` so it can `await` the charge: |
| 133 | + |
| 134 | + ```python |
| 135 | + @server.tool() |
| 136 | + async def add(a: float, b: float) -> float: |
| 137 | + """Add two numbers and return the sum.""" |
| 138 | + await Actor.charge(event_name='tool-call') |
| 139 | + return a + b |
| 140 | + ``` |
| 141 | + |
| 142 | +You can charge per tool call, per resource read, or per any other operation. For the full setup, see the [pay-per-event](../concepts/pay-per-event) guide. |
| 143 | + |
| 144 | +## Conclusion |
| 145 | + |
| 146 | +In this guide, you learned how to deploy an MCP server as an Apify Actor. You can now write a server with FastMCP or wrap an existing one with a proxy, expose it over Actor Standby, connect MCP clients to it, and monetize it with pay-per-event. To get started, see the [Actor templates](https://apify.com/templates/categories/python). If you have questions or need assistance, feel free to reach out on our [GitHub](https://github.com/apify/apify-sdk-python) or join our [Discord community](https://discord.com/invite/jyEM2PRvMU). Happy building! |
| 147 | + |
| 148 | +## Additional resources |
| 149 | + |
| 150 | +- [Apify templates: MCP server](https://apify.com/templates/python-mcp-empty) |
| 151 | +- [Apify templates: MCP proxy](https://apify.com/templates/python-mcp-proxy) |
| 152 | +- [Apify: actor-templates repository](https://github.com/apify/actor-templates) |
| 153 | +- [Apify: MCP server documentation](https://docs.apify.com/platform/integrations/mcp) |
| 154 | +- [Model Context Protocol: Official documentation](https://modelcontextprotocol.io) |
| 155 | +- [FastMCP: Official documentation](https://gofastmcp.com/) |
| 156 | +- [Apify blog: What is the Model Context Protocol](https://blog.apify.com/what-is-model-context-protocol/) |
| 157 | +- [Apify blog: How to use MCP with Apify Actors](https://blog.apify.com/how-to-use-mcp/) |
0 commit comments