Tom Han
11/10/2025, 7:25 PMMarvin
11/10/2025, 7:25 PMMarvin
11/10/2025, 7:30 PMPREFECT_API_URL (and PREFECT_API_KEY if applicable) are set so the server can talk to your Prefect API.
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).Nate
11/10/2025, 7:48 PMNate
11/10/2025, 7:51 PM