<@ULVA73B9P> How does prefect plays with fastmcp?
# ask-marvin
t
@Marvin How does prefect plays with fastmcp?
m
thought for 313.0 seconds
Great question! Short answer: there isn’t an official, first-class integration between Prefect and FastMCP today. But they play together nicely in two common patterns: 1) Expose Prefect as MCP tools via a FastMCP server - You write a small FastMCP server that defines “tools” which trigger Prefect flows/deployments. - Any MCP-capable client (e.g., an LLM IDE, an agent framework) can then call those tools to run Prefect work. Example: a minimal FastMCP server that triggers a Prefect deployment - Assumes Prefect 3.x and your self-hosted API URL. - Make sure
PREFECT_API_URL
(and
PREFECT_API_KEY
if applicable) are set so the server can talk to your Prefect API.
Copy code
import os

# Point to your self-hosted Prefect API
os.environ.setdefault("PREFECT_API_URL", "http://<your-mac-mini>:4200/api")
# If auth is enabled:
# os.environ.setdefault("PREFECT_API_KEY", "<your-key>")

from fastmcp import FastMCP
from prefect.deployments import run_deployment

server = FastMCP(name="prefect-tools", instructions="Tools to trigger Prefect flows.")

@server.tool()
async def run_prefect_deployment(deployment_name: str, parameters: dict | None = None) -> str:
    """
    Trigger a Prefect deployment like 'my-flow/my-deployment'.
    Returns the created flow run ID.
    """
    flow_run = run_deployment(
        name=deployment_name,
        parameters=parameters,
        as_subflow=False,   # calling from outside a Prefect flow
        # timeout=None       # omit or set if you want to block until completion
    )
    return str(flow_run.id)

if __name__ == "__main__":
    server.run()
- On the Prefect side,
run_deployment
is the simplest programmatic entrypoint in 3.x: run_deployment docs 2) Use Marvin (Prefect’s agentic framework) to connect to MCP servers - Marvin has experimental MCP support. You can connect a Marvin agent to one or more MCP servers (including those built with FastMCP) so the agent can call tools exposed by FastMCP. - This is helpful if you want an AI agent to orchestrate Prefect work through MCP tools. Links for more background: - Model Context Protocol: modelcontextprotocol.io - Marvin repo (MCP support is experimental): PrefectHQ/marvin - Prefect’s AI overview: Prefect for AI teams A couple of tips for your setup - Since you’re self-hosting Prefect 3.4.22 on a Mac mini, set
PREFECT_API_URL
to your server’s URL (e.g.,
http://<host>:4200/api
). Add
PREFECT_API_KEY
only if you’ve enabled auth. - If your FastMCP tool will trigger longer-running flows, consider returning quickly (omit
timeout
) and have the client poll flow status via another tool (e.g., a “get_flow_run_state” tool that uses the Prefect client). Questions for you - Which direction are you aiming for? - A) Expose Prefect capabilities as FastMCP tools so MCP clients/agents can trigger flows? - B) Call FastMCP tools from inside Prefect flows? If you share your target direction, I can tailor a full example (including status polling, error handling, and auth).
n
hi @Tom Han - great question! you can expect more from us on this soon
a couple thoughts • check out the prefect MCP server: https://www.prefect.io/blog/a-prefect-mcp-server consider these patterns: • incrementally eating deterministic workflows written in prefect with agentic while loops enabled by FastMCP servers • writing prefect workflows that happen to maintain state for MCP servers (e.g. docs MCP where you write a flow to update the vectorstore)