Build an MCP server¶
A Snowflake App Runtime app can serve its own tools over the Model Context Protocol (MCP). Agents such as Snowflake CoCo then call the app’s queries and actions directly, without opening its UI. The app keeps a single sign-in, a single set of grants, and a single copy of its query code for both people and agents.
The same tools can also power a chat assistant inside the app. See Build a chat assistant with Snowflake App Runtime.
Before you begin¶
- An app that runs on App Runtime. See Build a Node.js app for Snowflake App Runtime or Build a Python app for Snowflake App Runtime.
- CoCo CLI or CoCo Desktop to test the server.
How the server fits in the app¶
Follow these rules in any language:
- Serve one endpoint at
/api/mcp. A fixed path keeps the URL predictable for anyone who connects. - Run stateless Streamable HTTP. Create the server and transport for each request and don’t issue session IDs. App Runtime can run more than one instance and can restart the app, so state held in memory between requests is lost or split across instances.
- Accept only
POST. AnswerGETandDELETEwith405. In stateless mode there’s no stream or session for them to open or close, and an SDK that receives aGETholds it open as an empty event stream. - Return JSON, not an event stream, when the SDK lets you choose. Each call returns one result, and a JSON body is easier to debug.
- Don’t add your own authentication. The App Runtime ingress signs the caller in before the request reaches your app, as it does for every other route, and adds the caller’s identity headers. Don’t add an API key check or an OAuth flow.
Choose whose rights each tool uses¶
MCP requests come through the same ingress as browser requests, so each tool runs queries with owner’s rights or caller’s rights, as your other routes do:
- Owner’s rights give every caller what the app’s owner role can read. Don’t expose a tool that the UI keeps behind an admin check unless the tool makes the same check. Keep write and delete actions off MCP unless you intend agents to perform them.
- Caller’s rights run as the signed-in user. Use them for data protected by row access or masking policies, or for per-user data. If a request doesn’t carry the caller’s token, fail the call. Don’t fall back to owner’s rights.
Add an MCP server to a Node.js app¶
These steps use a Next.js app built from the App Runtime template. To run it
locally, see Run locally.
For Express, use StreamableHTTPServerTransport, the Node.js http variant of the
transport.
-
Install the MCP SDK and Zod, which describes each tool’s arguments:
-
Create
lib/tools.tswith your tools. Each tool has a name, a description, an argument schema, and a function that calls the same queries your pages and API routes use. The MCP server and a chat assistant can both read this list:For a caller’s rights tool, query with
await asCaller()from@/lib/snowflakeinstead ofsnowflake. Outside the template, usesnowflake.asCaller({ headers: request.headers }). See Query from Node.js. -
Create
app/api/mcp/route.ts. It registers every tool in the list and returns errors as tool results:
Add an MCP server to a Python app¶
These steps add a server to the FastAPI app from
Build a Python app for Snowflake App Runtime. Python apps are in
Public Preview. The steps use version 2 of the mcp package, which renamed
FastMCP to MCPServer. Code written for version 1 imports
mcp.server.fastmcp and fails on version 2.
-
Add the SDK to
requirements.txt: -
Add the server to
app.py. Put the imports below thesys.path.insertline, becausemcpinstalls into.python_packages. Replaceapp = FastAPI()with theapp = FastAPI(lifespan=lifespan)line below, and keep your/route and theuvicorn.runblock.run_querystands for your app’s query helper; see Query from Python.list_ordersruns with owner’s rights. -
To run a tool with caller’s rights, add a
Contextargument. Themcppackage fills it in and leaves it out of the tool’s schema, andctx.headersholds the request’s headers, including the caller token.connectandcaller_tokencome from Query from Python:
Important
By default, the mcp package rejects any Host header other than
localhost with 421 Invalid Host header. Behind the App Runtime ingress,
every request carries the app’s public host name, so the server works on your
machine and fails once deployed. Turn off the check as shown, or set
allowed_hosts to the app’s exact host name. Wildcards such as *.domain
don’t match. The ingress authenticates every request, so the check isn’t
needed in a deployed app.
Share tools with in-app chat¶
If your app has a chat assistant, give it the same tools as the MCP server:
- Define each tool once. Keep one list, as in
lib/tools.tsabove, and have the MCP route and the chat route both read it. Agents and the in-app chat then get the same descriptions, limits, and answers. - Mark tools that stay in the app. A tool that needs an admin check, or that writes data, can stay in chat behind the app’s own checks and off the MCP server. Filter it out when you register MCP tools.
- Apply the same limits in both places. Cap rows in the shared tool, not
in each route, so both models see the same rows. The chat route also cuts
long results to fit the model’s context, with
MAX_RESULT_CHARS.
For the chat side, see Build a chat assistant with Snowflake App Runtime.
Write tools that agents can use¶
- Reuse the app’s query code. A tool calls the same helper as the page or route that shows the same data, so agents and the UI get the same numbers. Don’t open a second connection or copy SQL.
- Bind every argument. A model writes the tool arguments. Pass them as bind variables, never in the SQL text.
- Describe tools for a model. Say what the tool returns, the units, and when to use it. Give each argument a type, and enums and limits where they apply.
- Limit results. Cap rows (for example, at 200) and say in the result when you cut it. Return dates as ISO 8601 strings.
- Return errors as tool results. Set
isError: truewith a short message instead of returning HTTP 500, so the agent can read the error and recover. Log the full error in the app. - Log one line for each request with the method, tool name, status, and duration. The app logs are the only record of what an agent called.
Test the server¶
CoCo CLI and CoCo Desktop both connect to the server. They read the same
~/.snowflake/cortex/mcp.json file, so a server you add with cortex mcp add
appears in both. To manage servers, see
CoCo CLI Model Context Protocol (MCP) support and
MCP support in CoCo Desktop.
-
Run the app locally and list its tools. Replace
<port>with the port the app listens on:Then call a tool with
"method":"tools/call","params":{"name":"<tool>","arguments":{...}}and check that it returns real data. -
Add the local server to CoCo, and try the tools from an agent before you deploy. A local server needs no sign-in:
In CoCo CLI, new servers start turned off: turn the server on in
/mcp.On your machine, the Node.js SDK runs queries as the default connection in your Snowflake CLI configuration, including caller’s rights queries. A Python app needs its own local connection, because the session token file exists only in a deployed app.
-
Deploy the app, and find its URL in the output of SHOW APPLICATION SERVICES.
-
Add the deployed server.
--snowflake-connectionnames the connection that CoCo signs in with, so you don’t create or store a token:In
mcp.json, the entry looks like this:The tools appear in your next CoCo session. Turn the server on in
/mcpin CoCo CLI, as for the local server.
Share the server¶
Anyone who connects to the server needs the same access as someone who opens the app in a browser. See Share a Snowflake App Runtime app.