{"catalogRevision":"cat_726f873af6f566cc","recipeId":"page:recipes:mcp-server:fastapi:worker-state","recipeRevision":"r1","canonicalPath":"/recipes/mcp-server/fastapi/worker-state","label":"Host mcp server with FastAPI: worker execution and job state","title":"How to host an MCP server built with FastAPI on Ample: worker execution and job state","family":"Workload recipe","summary":"Deploy an MCP server built with FastAPI as a public web service plus a dedicated worker process on Ample. `ample plan --write` discovers both services and their shared database and writes ample.toml; `ample up` reconciles the topology: a managed PostgreSQL 16 database, a public web microVM and a private worker microVM (kind worker, no public URL), both reading DATABASE_URL. Verified on FastAPI: a separately deployed worker service (kind worker, no public URL) sharing a declared managed PostgreSQL database with the web service, consuming a jobs table with row locks and marking jobs done once (worker=done attempts=1). The MCP server-specific behavior (your job payloads, retry policy and scheduling) is your application code on that verified basis.","representativeQueries":["Host mcp server with FastAPI: worker execution and job state","Where can I host MCP server built with FastAPI?","I need a separately runnable worker, durable job records and idempotent retries; not a managed queue."],"breadcrumbIds":["discovery","workload","workload:agents-and-backends","workload:mcp-server","page:recipes:mcp-server:fastapi:worker-state"],"breadcrumbs":[{"id":"discovery","label":"Agent hosting discovery","canonicalPath":"/discover/discovery","kind":"root"},{"id":"workload","label":"Applications and workloads","canonicalPath":"/discover/workload","kind":"facet"},{"id":"workload:agents-and-backends","label":"Agents and backends","canonicalPath":"/discover/workload/agents-and-backends","kind":"group"},{"id":"workload:mcp-server","label":"MCP server","canonicalPath":"/discover/workload/mcp-server","kind":"atom"},{"id":"page:recipes:mcp-server:fastapi:worker-state","label":"Host mcp server with FastAPI: worker execution and job state","canonicalPath":"/recipes/mcp-server/fastapi/worker-state","kind":"recipe"}],"parentIds":["workload:mcp-server","stack:framework-fastapi","pattern:dedicated-worker-process","intent:deploy-mcp-server"],"parents":[{"id":"workload:mcp-server","label":"MCP server","canonicalPath":"/discover/workload/mcp-server","kind":"atom"},{"id":"stack:framework-fastapi","label":"FastAPI","canonicalPath":"/discover/stack/framework-fastapi","kind":"atom"},{"id":"pattern:dedicated-worker-process","label":"Dedicated worker process","canonicalPath":"/discover/pattern/dedicated-worker-process","kind":"atom"},{"id":"intent:deploy-mcp-server","label":"Deploy MCP server","canonicalPath":"/discover/intent/deploy-mcp-server","kind":"atom"}],"resourceRequirements":["primitive:compute","primitive:postgres"],"infrastructureRequirements":[{"primitiveId":"primitive:compute","label":"Compute","canonicalPath":"/discover/primitive/compute","status":"verified","summary":"Apps run in isolated x86_64 Firecracker microVMs; sizes are the priced VM sizes."},{"primitiveId":"primitive:postgres","label":"Postgres","canonicalPath":"/discover/primitive/postgres","status":"verified","summary":"Managed PostgreSQL 16 with pgvector runs in its own microVM and is auto-provisioned when an app needs a database and no DATABASE_URL is supplied."}],"framework":"FastAPI","workload":"MCP server","releaseStatus":"published","supportStatus":"verified","executionStatus":"ready","docsOnly":false,"prerequisites":["A repository with the web app and the worker as separate service directories (the fixture uses apps/web and apps/worker), each building and starting with the documented commands (pip install into .ample/python from requirements.txt, then uvicorn from run.py reading PORT on the python-3.12 template; the worker starts with python3 worker.py and binds no port)","Both services read DATABASE_URL at runtime and share one jobs table; the worker claims rows with SELECT ... FOR UPDATE SKIP LOCKED and marks them done once","An Ample account token with servers:write and databases:write (ample up creates the database)"],"testedConfiguration":{"template":"python-3.12","runtime":"python","size":"s-1vcpu-1gb","install":"python3 -m pip install --target .ample/python -r requirements.txt","start":"PYTHONPATH=.ample/python:${PYTHONPATH:-} python3 run.py"},"inputSchema":{"additionalProperties":false,"properties":{"path":{"description":"Directory containing ample.toml (the whole topology is applied)","maxLength":512,"minLength":1,"type":"string"},"wait_secs":{"description":"Maximum seconds to wait for each service deployment","maximum":3600,"minimum":30,"type":"integer"}},"required":["path"],"type":"object"},"outputSchema":null,"workflowSteps":[{"title":"Plan the topology","body":"Run the planner once. It discovers the web and worker services, infers kind = \"worker\" for the process without a port, declares the database each service needs, and writes ample.toml. Exit 2 means it left questions in the manifest; answer them with --answer or by editing the file.","command":"ample plan --write ."},{"title":"Share one database","body":"Keep a single [databases.main] with engine = \"postgres\", drop any per-service database the planner added, and set DATABASE_URL = { database = \"main\" } under both [services.web.env] and [services.worker.env]. Python services also need an explicit start command in the manifest (the fixture sets start for both services).","command":"ample plan --offline ."},{"title":"Apply","body":"Reconcile the whole manifest in dependency order: the database first, then both services. It is idempotent and never destructive; exit 0 means everything applied, 2 needs input, 1 a failed resource (independent siblings still proceed and re-running resumes).","command":"ample up ."},{"title":"Verify","body":"Fetch the web service URL and its worker status route (/p/worker-queue/status in the example) and confirm the worker processed the enqueued job; on failure read the worker service's runtime logs.","command":"ample logs <deployment_id> --kind runtime"}],"examples":[{"title":"FastAPI web + worker fixture","description":"A FastAPI web app (apps/web) and a worker process (apps/worker) sharing one managed PostgreSQL database; the queue module is under tests/deploy-canaries/_pattern-modules.","sourceRef":"tests/deploy-canaries/fastapi-worker"}],"successChecks":[{"description":"web service responds on its public URL","kind":"http_get","path":"/","expect":"ample canary fastapi patterns"},{"description":"worker consumed the enqueued job","kind":"http_get","path":"/p/worker-queue/status","expect":"worker=done attempts=1 result=processed-by-worker"}],"limitations":["Verified on the python-3.12 template at s-1vcpu-1gb for both services with the example fixture.","The worker-queue check verified the platform side; the MCP server itself (your job payloads, retry policy and scheduling) is your application code.","The worker is a long-running process supervised in its own microVM and wakes with the web service that feeds it; for scheduled work add an `ample job` (`[jobs.<name>]` in ample.toml).","The worker service is private: it has no public URL and takes its jobs from the shared database; both services are placed with their database.","Managed PostgreSQL 16 with pgvector; apps and their databases are placed together."],"costEstimate":{"currency":"USD","monthlyAmount":15.0,"authoritative":true,"basis":"size prices from pricing.toml (loaded by the API) at build revision 3a3dfe06151688a20d397caac2aa66e562c23eb0","components":[{"name":"web server","size":"s-1vcpu-1gb","quantity":1.0,"monthlyAmount":5.0},{"name":"worker server","size":"s-1vcpu-1gb","quantity":1.0,"monthlyAmount":5.0},{"name":"managed PostgreSQL database","size":"s-1vcpu-1gb","quantity":1.0,"monthlyAmount":5.0}],"note":"Upper-bound monthly price of the tested sizes for the web service, the worker service and the database."},"evidenceSummary":[{"kind":"canary_run","summary":"FastAPI web service plus a separate Python worker process deployed on Ample with `ample plan --write` then `ample up` (pip install into .ample/python from requirements.txt, then uvicorn from run.py reading PORT on the python-3.12 template); the web service enqueued a job that the private worker service consumed from the shared managed PostgreSQL database. Pattern proof: a separately deployed worker service (kind worker, no public URL) sharing a declared managed PostgreSQL database with the web service, consuming a jobs table with row locks and marking jobs done once (worker=done attempts=1).","observedAt":"2026-11-05T22:28:59Z","implementationRevision":"da3e37255e5a","expiresAt":"2027-05-04T22:28:59Z","scope":{"checks":["/","/p/worker-queue/status"],"pattern":"pattern:dedicated-worker-process","services":["web","worker"],"template":"python-3.12","tool":"ample_up"}}],"lastVerifiedAt":"2026-11-05T22:28:59Z","mcpBinding":{"registryRef":"mcp:ample_up","toolName":"ample_up","schemaHash":"29c90695d7a0dc2d5cdc78dcc2bc3002d38ee2e0601aa6ad5418ef6690d6de11","observedAt":"2026-11-06T06:16:17.674112535+00:00","implementationRevision":"4558e9477d09","requiredScopes":["servers:write","databases:write"],"bindingState":"current"},"unknowns":[],"formats":{"html":"https://ample.computer/recipes/mcp-server/fastapi/worker-state","markdown":"https://ample.computer/recipes/mcp-server/fastapi/worker-state.md","json":"https://api.ample.computer/v1/catalog/recipes/page%3Arecipes%3Amcp-server%3Afastapi%3Aworker-state"},"nextActions":[{"actionId":"browse-catalog","label":"Browse the catalog index","operationId":"catalog_index","method":"GET","relativePath":"/v1/catalog","origin":"api","parameters":{},"requiresAuthentication":false,"requiresApproval":false},{"actionId":"search-recipes","label":"Search published recipes by intent, stack and constraints","operationId":"search_recipes","method":"POST","relativePath":"/v1/catalog/search","origin":"api","parameters":{"body":{"limit":5,"query":"Host mcp server with FastAPI: worker execution and job state"}},"requiresAuthentication":false,"requiresApproval":false},{"actionId":"plan:page:recipes:mcp-server:fastapi:worker-state","label":"Prepare a side-effect-free deployment plan for an authorized project","operationId":"plan_deployment","method":"POST","relativePath":"/v1/catalog/plan","origin":"api","parameters":{"body":{"inputs":{},"projectId":"<workspace or server id you own>","recipeId":"page:recipes:mcp-server:fastapi:worker-state","recipeRevision":"r1"}},"requiresAuthentication":true,"requiresApproval":false},{"actionId":"auth-setup","label":"Read the existing agent authentication setup","operationId":"existing_auth_setup","method":"GET","relativePath":"/mcp/setup","origin":"api","parameters":{},"requiresAuthentication":false,"requiresApproval":false},{"actionId":"browse:workload:mcp-server","label":"Browse MCP server","operationId":"browse_node","method":"GET","relativePath":"/v1/catalog/nodes/workload%3Amcp-server","origin":"api","parameters":{"nodeId":"workload:mcp-server"},"requiresAuthentication":false,"requiresApproval":false},{"actionId":"browse:stack:framework-fastapi","label":"Browse FastAPI","operationId":"browse_node","method":"GET","relativePath":"/v1/catalog/nodes/stack%3Aframework-fastapi","origin":"api","parameters":{"nodeId":"stack:framework-fastapi"},"requiresAuthentication":false,"requiresApproval":false},{"actionId":"browse:pattern:dedicated-worker-process","label":"Browse Dedicated worker process","operationId":"browse_node","method":"GET","relativePath":"/v1/catalog/nodes/pattern%3Adedicated-worker-process","origin":"api","parameters":{"nodeId":"pattern:dedicated-worker-process"},"requiresAuthentication":false,"requiresApproval":false},{"actionId":"browse:intent:deploy-mcp-server","label":"Browse Deploy MCP server","operationId":"browse_node","method":"GET","relativePath":"/v1/catalog/nodes/intent%3Adeploy-mcp-server","origin":"api","parameters":{"nodeId":"intent:deploy-mcp-server"},"requiresAuthentication":false,"requiresApproval":false}]}