Skip to main content

Pair Your Agent With the Documentation, Not Just the Warehouse

datamcp gives an agent two tools and no service account: your dbt docs as generated markdown, and read-only SQL as the person asking. The tradeoff, and why we skip the metric tool.

By Marco Porracin
Model: Grok
#datamcp#MCP#dbt#Snowflake#OAuth#AI agents#data platform

The short version

Most "chat with your warehouse" setups have the same quiet flaw: the agent connects as one service account, and every user inherits its permissions. You end up bolting a second permission model onto a warehouse that already has one.

datamcp is the pattern we run instead. Two tools, no service account. One returns your dbt documentation as markdown so the agent knows what the business means. The other runs read-only SQL as the person asking, under their own Snowflake identity and grants. There is no shared credential to leak, because there is no shared credential.

We run this in production. This post is how it works, and the tradeoff we accepted to get there.

Two tools

The MCP server is a small FastAPI app that is both an OAuth authorization server and an MCP resource server, exposed at a single URL. It registers exactly two tools:

  • get_dwh_mcp_context returns the marts and metrics catalog as a markdown blob, built by parsing the dbt manifest.json.
  • run_snowflake_query runs one read-only SQL statement, under your own Snowflake identity.

That is the whole surface. It is deliberately small, and the smallness is the design: every tool you add is another thing an agent can do in a user's session.

The context tool's description tells the client to call it at the start of a session, before writing any SQL. In practice that is what happens: the agent reads the catalog, finds the mart relevant to the question, and writes against a relation it can see documented, rather than guessing at table names until something returns rows.

The context is generated, not maintained

The obvious way to build this is to write a document about your warehouse. The obvious way fails, because the document drifts: someone renames a column, adds a mart, changes a description, and the document is now a confident liar.

So the catalog is generated from the artifact that does not drift by hand, the dbt manifest. Our CI publishes manifest.json to S3 after every production build, and the MCP server reads it from there. The parser walks the models materialized under models/marts/, pulling relation, description, columns and their descriptions, meta.owner, and tags. It also reads semantic_models and metrics from the same manifest into a lightweight metric catalog.

Two properties fall out of that:

  • It refreshes without a deploy. The index is cached in memory with a TTL, so merging a mart shows up in the tool's output after the TTL expires. No code change, no restart.
  • Descriptions come from the .yml files your team already writes. If a column description is missing in dbt, it is missing in the catalog, and the agent will see a gap rather than an invention. Documentation stops being a separate chore and becomes the same file you were already maintaining.

The parser covers marts only, not staging, not intermediate or other layers, which is also by design. The agent is meant to build on curated models, and a catalog that lists everything invites it to join staging tables it does not understand.

There is also a hand-maintained business_rules.md, merged into the same output, with a rule at the top about what belongs in it: only caveats that cannot be derived from a mart's own description. In practice those are population definitions and well-known traps. A funnel where each stage is a subset of the one before it, rather than six independent counts. A column whose name suggests one thing and whose semantics are another. The kinds of thing that live in one person's head and nowhere else.

The metric tool we deliberately did not build

The obvious next tool is a metric runner: pass a metric name and a grain, get a number back from MetricFlow. We did not build it. MetricFlow needs a fixed warehouse identity to run as, and this pattern has none: every query already runs as the person asking. So the metrics in the catalog are documentation, not an execution API. The agent reads the definition and writes the SQL itself, under that person's grants. That is a real tradeoff, which we come back to below. (If you want the other side of this, see Your agent is only as good as your semantic layer.)

The warehouse is the permission model

The auth flow is the interesting engineering. The server acts as a confidential OAuth client of a Snowflake custom-client security integration, so login happens against Snowflake itself:

  1. The MCP client registers with the server via Dynamic Client Registration: open, because MCP clients self-provision. Client records expire on a TTL rather than being gated by a shared secret.
  2. The client sends the user to the server's /authorize, with PKCE.
  3. The server redirects to Snowflake's own /oauth/authorize.
  4. The user signs in to Snowflake. Not to us. To their warehouse.
  5. The server exchanges the code, stores the Snowflake access and refresh tokens, and connects with authenticator="oauth".

From that point, every query runs as that person. The integration activates every role the caller holds as a secondary role, not just their DEFAULT_ROLE, so privileges granted through a legacy role apply automatically without a USE ROLE detour.

The consequences are the useful part:

  • There is no shared service credential in the code or the environment. Each session uses that person's Snowflake OAuth tokens, not a warehouse-wide key. A server compromise is still serious, because those tokens are stored so the agent can run queries, but it does not yield one standing credential that can act as every user at once.
  • Onboarding and offboarding are Snowflake operations. A new analyst gets access because they exist in Snowflake with the right grants. Someone leaving loses warehouse access as they already would, and the agent stops being able to do anything for them the same second.
  • Errors are honest. A caller with no Snowflake user, or one lacking a grant, gets exactly the error their own session would produce. We surface those messages verbatim instead of translating them into a friendly "you don't have access", because the real message tells them which role is missing.

That last point is underrated. It means the MCP server has no permission logic of its own to get wrong. It is a thin adapter over an identity system that has been hardened for years.

The guardrails that are still ours

Delegating identity to Snowflake does not mean trusting SQL blindly. The remaining risk is a read-only credential being used for something expensive or destructive, so the server validates statements before they run.

Parsing is done with sqlglot, which gives a statement type rather than a regex match. The allowlist is explicit: SELECT, UNION, DESCRIBE, USE, SHOW, EXPLAIN are permitted; ALTER, CREATE, DROP, INSERT, UPDATE, DELETE, MERGE, COPY, TRUNCATE, and transactions are not. The catch-all Command type, which is where CALL, EXECUTE IMMEDIATE, GRANT, ALTER USER and anything unparseable lands, is off, and stays off.

USE gets a second check, because it is the only permitted statement that changes session state: only DATABASE, SCHEMA and WAREHOUSE are allowed, and USE ROLE is always rejected regardless of configuration. Letting a caller switch roles would defeat the point of the OAuth identity.

Results are capped at a fixed row limit, and the response carries a truncated flag. That is not a surprise to anyone reading the response: the tool description says so, and tells the agent to narrow the query and re-run. You get an agent that paginates honestly instead of an agent that silently reports the first page of rows as the total.

Where this pattern hits its limit

Three honest limits, because this is not free.

It needs a warehouse that treats people as identities. Snowflake's OAuth integration is what makes this work. If your warehouse has one shared login and permissions managed in a spreadsheet, this pattern has nothing to delegate to, and you would be back to a service account with extra steps.

Marts are the product, and they need to exist. The context tool can only describe what dbt knows. If your transformation layer is a pile of SELECT * statements with no descriptions, the catalog is empty and the agent is back to guessing. The tool makes an existing modeling discipline legible; it does not create one.

There is still no metric execution path. That was a deliberate trade, and it has a cost: an agent can write a subtly wrong metric SQL even with a good description. The mitigation is the mart, not the tool. If the metric logic lives in a governed mart, writing the query wrong is harder. If it lives in five dashboards, the agent will reproduce the ambiguity faithfully.

A small first version

If you want to run this on your own stack, the sequence that matters:

  1. Sort out identity first. Create a Snowflake security integration for OAuth and confirm a real user can authenticate and see their grants. Everything else is scaffolding over this.
  2. Start with the context tool alone. Generate it from the manifest, point an agent at it, and ask it questions. You will learn quickly whether your documentation is worth serving.
  3. Add the read-only query tool with the allowlist on. Verify each rejected statement type with a concrete test: the thing that matters is the parse result, not that a regex matched.
  4. Pin the curated layer. Marts only, no staging, and a hand-written rules file for the caveats that cannot be derived.
  5. Pair it with a BI MCP. This is how we use it. Metabase's MCP server gives the agent access to the questions, models and dashboards your team already reads; datamcp gives it the definitions and a way to run SQL under the user's own identity. Together they let a business user ask a question and get a report against a curated mart, without a copy of the repo on their laptop. The SQL path still uses their Snowflake login, not a shared warehouse password stored in the agent.

Step five is the one that changes who can use the data. Steps one to four are the reason it is safe to let them.

Pair the agent with the documentation, not just the warehouse

The instinct with MCP is to give the model more tools. We have found the opposite works better: give it fewer tools, and make the two it has unusually trustworthy.

Once the catalog refreshes, an agent that reads generated documentation cannot describe a mart that was deleted last week. An agent that queries as the person asking cannot return a row that person could not have selected themselves. Neither property comes from better prompting. Both come from refusing to give the agent its own identity.

If you are wiring an agent into a warehouse and want a second opinion on the identity model, or on whether your marts are documented well enough to be served, talk with us. We can scope the smallest useful version and tell you honestly whether the foundation is there yet.

RSS Feed

Prefer RSS? Subscribe to our RSS feed to get updates directly in your feed reader.

Subscribe to RSS

Want to talk through a similar data problem?

If this is close to the kind of work your team needs, request a conversation and tell us what you are trying to solve.

Request a Conversation