|
| 1 | +"""Runnable example: a Claude Agent SDK agent against the Marmot MCP server. |
| 2 | +
|
| 3 | +Auto-registers the agent as a Marmot Agent asset and writes lineage edges for |
| 4 | +every catalog tool the agent calls. Mirrors `sdk/ts/examples/claude-agent-marmot`. |
| 5 | +
|
| 6 | +Usage: |
| 7 | + pip install marmot-sdk[claude-agent] |
| 8 | + python main.py [prompt...] |
| 9 | +
|
| 10 | +The Marmot host + credential is resolved via the SDK's standard chain: |
| 11 | +explicit kwargs → MARMOT_HOST/MARMOT_API_KEY/MARMOT_TOKEN env vars → |
| 12 | +~/.config/marmot/credentials.json (populated by `marmot login`) → workload |
| 13 | +identity. |
| 14 | +""" |
| 15 | + |
| 16 | +from __future__ import annotations |
| 17 | + |
| 18 | +import asyncio |
| 19 | +import sys |
| 20 | + |
| 21 | +from claude_agent_sdk import ClaudeAgentOptions, ClaudeSDKClient |
| 22 | + |
| 23 | +from marmot import Client, resolve |
| 24 | +from marmot.integrations.claude_agent import MarmotAgentTracker |
| 25 | + |
| 26 | +DEFAULT_PROMPT = ( |
| 27 | + "Use the Marmot catalog tools to find a postgres table related to orders. " |
| 28 | + "Reply with one sentence summarising the top hit and quoting its MRN." |
| 29 | +) |
| 30 | + |
| 31 | + |
| 32 | +async def main() -> None: |
| 33 | + prompt = " ".join(sys.argv[1:]) or DEFAULT_PROMPT |
| 34 | + |
| 35 | + base_url, credential = resolve(base_url=None) |
| 36 | + client = Client(base_url=base_url, credential=credential) |
| 37 | + tracker = MarmotAgentTracker( |
| 38 | + client, |
| 39 | + name="catalog-explorer-claude-py", |
| 40 | + model="claude-sonnet-4-5", |
| 41 | + owner="data-eng", |
| 42 | + ) |
| 43 | + |
| 44 | + mcp_headers = ( |
| 45 | + {"Authorization": f"Bearer {credential.token}"} |
| 46 | + if credential.scheme == "Bearer" |
| 47 | + else {"X-API-Key": credential.token} |
| 48 | + ) |
| 49 | + options = ClaudeAgentOptions( |
| 50 | + mcp_servers={ |
| 51 | + "marmot": { |
| 52 | + "type": "http", |
| 53 | + "url": f"{base_url}/api/v1/mcp", |
| 54 | + "headers": mcp_headers, |
| 55 | + } |
| 56 | + }, |
| 57 | + hooks=tracker.hooks(), |
| 58 | + permission_mode="bypassPermissions", |
| 59 | + allowed_tools=[ |
| 60 | + "mcp__marmot__discover_data", |
| 61 | + "mcp__marmot__find_ownership", |
| 62 | + "mcp__marmot__lookup_term", |
| 63 | + ], |
| 64 | + ) |
| 65 | + |
| 66 | + print(f"Marmot host: {base_url} (auth via {credential.source})") |
| 67 | + print(f"Prompt: {prompt}\n") |
| 68 | + |
| 69 | + async with ClaudeSDKClient(options=options) as agent: |
| 70 | + await agent.query(prompt) |
| 71 | + async for msg in agent.receive_response(): |
| 72 | + blocks = getattr(getattr(msg, "message", None), "content", None) or [] |
| 73 | + for block in blocks: |
| 74 | + text = getattr(block, "text", None) |
| 75 | + if isinstance(text, str) and text: |
| 76 | + print(text) |
| 77 | + |
| 78 | + print("\nagent registered as:", tracker.agent_mrn or "(not yet registered)") |
| 79 | + |
| 80 | + |
| 81 | +if __name__ == "__main__": |
| 82 | + asyncio.run(main()) |
0 commit comments