Builds on Day 2's Lakebase pattern. Day 3 adds:
- An Alpaca Markets paper-trading MCP server (
mcp_server/) - exposes paper-trading tools (get_quote,place_trade,get_positions,get_account_summary,get_order_history) over the Model Context Protocol, backed by a real Alpaca paper-trading account. - A Databricks Agent Bricks agent that connects to that MCP server as an external tool, reads market data from your Lakebase Day 2 watchlist/news tables, and decides to place paper trades against your real (but fake-money) Alpaca account.
- A small dashboard app (
dashboard/) to watch those trades land in near real time.
Why Alpaca? Alpaca Markets provides a free, real, hosted paper-trading environment with a clean Python SDK (alpaca-py) and no lengthy manual app approval process, so students get real market data and real (simulated-money) order fills without any risk of real money moving. See "Setting up Alpaca Markets" below to create your paper account and API keys.
Agent Bricks agent --(MCP tool calls)--> mcp_server/alpaca_mcp_server.py --(REST)--> Alpaca Markets (paper)
^
| (reads context: watchlist, ticker_news_* from Day 2 Lakebase)
+-----------------------------------------------------------------------------------+
|
dashboard/app.py <--(reads same Alpaca account)------+
mcp_server/anddashboard/are two separate Databricks Apps - one serves MCP tool calls to the agent, the other serves a human-facing dashboard. Both read/write the exact same Alpaca paper-trading account (via their own copy ofalpaca_broker.py), so trades placed by the agent through MCP show up in the dashboard immediately, and vice versa.mcp_server/alpaca_broker.pyis the broker adapter: it wrapsalpaca-py'sTradingClient(orders, positions, account info) andStockHistoricalDataClient(quotes) to implement the 5 functions the MCP tools call. There's no local simulation - quotes, fills, cash, and positions all come straight from Alpaca's paper-trading API.mcp_server/alpaca_mcp_server.pywrapsalpaca_broker.pywith FastMCP@mcp.tooldecorators and serves them over streamable HTTP - the transport Databricks' MCP client/gateway expects when you host your own MCP server as a Databricks App.- Alpaca's paper trading is one account per API key pair, not multi-tenant -
account_idis accepted by every tool for signature compatibility but doesn't select between accounts; every call operates against the single Alpaca paper account configured via secrets (see below).
mcp_server/alpaca_mcp_server.py- FastMCP server exposing the 5 paper-trading toolsmcp_server/alpaca_broker.py- Broker adapter wrapping Alpaca'salpaca-pySDKmcp_server/paper_broker.py/mcp_server/lakebase.py- legacy Lakebase-simulated engine, kept for reference/fallback only (no longer imported)mcp_server/app.yaml/mcp_server/requirements.txt- Databricks App config for the MCP serverdashboard/app.py- Flask dashboard (read-only view of the Alpaca paper account)dashboard/templates/index.html- Dashboard UI (cash, positions, P/L, recent orders)dashboard/alpaca_broker.py- copy of the same broker adapter (each Databricks App deploys from its own folder, so each needs its own copy of shared code)dashboard/paper_broker.py/dashboard/lakebase.py- legacy Lakebase-simulated engine, kept for reference/fallback only (no longer imported)dashboard/app.yaml/dashboard/requirements.txt- Databricks App config for the dashboardsetup_secrets.py- One-time script to store the Lakebase URL secret (same as Day 2; still used if you keep Day 2's watchlist/news tables for agent context).env.example- Local dev env var template
Both apps need an Alpaca paper-trading API key ID and secret key, stored as Databricks secrets (never committed to the repo).
Sign up at alpaca.markets (no funding or brokerage approval needed for paper trading - it's instant, unlike a real brokerage app).
- Log in to the Alpaca dashboard.
- Make sure you're viewing Paper Trading (there's a live/paper toggle in the dashboard) - never use live-trading keys for this lab.
- Under API Keys, generate a new key pair. Copy the Key ID and Secret Key immediately - the secret is only shown once.
From a Databricks notebook or the CLI, base64-encode and store both values (same pattern as the Lakebase URL secret):
databricks secrets put-secret database alpaca-key-id --string-value "$(echo -n YOUR_KEY_ID | base64)"
databricks secrets put-secret database alpaca-secret-key --string-value "$(echo -n YOUR_SECRET_KEY | base64)"If you use a different secret scope than database, update ALPACA_SECRET_SCOPE in both
mcp_server/app.yaml and dashboard/app.yaml to match.
For running the apps locally without Databricks secrets, alpaca_broker.py still reads through
WorkspaceClient().secrets.get_secret(), so local runs need a Databricks CLI profile configured
with access to the secret scope above (databricks auth login), or you can temporarily hardcode
test keys - just never commit them.
Lakebase is still used for agent context (Day 2's watchlist/ticker_news_* tables) even
though trading now goes through Alpaca. If you already have a Lakebase instance from Day 2,
reuse it. Otherwise, follow
Day 2's step 2
to create one.
- Lakebase URL: from a Databricks notebook (
%sh python setup_secrets.py), same as Day 2. - Alpaca API keys: see "Setting up Alpaca Markets" above.
cp .env.example .env
# paste your Lakebase URL into LAKEBASE_URLcd mcp_server && pip install -r requirements.txt && python alpaca_mcp_server.py # serves MCP on :8000In a second terminal:
cd dashboard && pip install -r requirements.txt && python app.py # serves UI on :8001Open http://localhost:8001 to see your Alpaca paper account (starting cash, no positions
yet). Use an MCP Inspector
or curl against http://localhost:8000 to sanity-check the tools before deploying.
Following Day 2's step 7 (Git folder + Apps UI, no CLI needed), but this time deploy two apps pointed at two different subfolders of the same Git folder:
- Create a Git folder for this repo (once) as in Day 2.
- Deploy the MCP server app: Compute > Apps > Create app > Custom, name it e.g.
alpaca-paper-mcp, and point its source at the Git folder'sdatabricks-lakebase-app-day-3/mcp_server/subfolder (so it picks upmcp_server/app.yaml). Deploy it, then copy its app URL - you'll register that URL as an external MCP server in step 6. - Deploy the dashboard app: repeat, naming it e.g.
paper-trading-dashboard, pointing atdatabricks-lakebase-app-day-3/dashboard/. Deploy it and open its URL to confirm the dashboard loads and shows your Alpaca account.
Follow Connect agents to external MCPs and tools:
- In your workspace, go to AI Gateway > MCPs > Add MCP (or Register external MCP).
- Paste the
alpaca-paper-mcpapp's URL from step 5 as the server endpoint (streamable HTTP). - Give it a name (e.g.
alpaca-paper-trading) and save. Databricks will introspect the server and list the 5 tools (get_quote,place_trade,get_positions,get_account_summary,get_order_history). - Grant your Agent Bricks agent (created next) access to this MCP server via Unity Catalog permissions, if prompted.
-
In your workspace sidebar, go to Agents > Agent Bricks > Create agent.
-
Choose the Custom LLM (or Multi-agent supervisor, if you want to combine this with a research agent) agent type - either works for a single tool-calling agent like this.
-
Under Tools, add:
- The
alpaca-paper-tradingMCP server you registered in step 6 (all 5 tools, or a curated subset - e.g. leave outplace_tradefor a "research-only" version of the agent first, then add it back once you trust the guardrails). - Optionally, a Unity Catalog function tool or Genie space wired to your Day 2
watchlist/ticker_news_documents/ticker_news_embeddingstables, so the agent has real context (tracked tickers + recent news/sentiment) to reason about before trading.
- The
-
Give the agent a system prompt along the lines of:
You are a paper-trading research assistant. Use
get_account_summaryto check current cash/positions before proposing a trade. Use the watchlist/news tools to justify any BUY or SELL. Always callget_quoteimmediately beforeplace_tradeto confirm price. Only trade symbols already on the watchlist. Never exceed 10% of account equity in a single order. Explain your reasoning before callingplace_trade. -
Evaluate and iterate: Agent Bricks auto-evaluates the agent against sample prompts (e.g. "Check AAPL and buy 10 shares if sentiment is positive") - use this to tune the system prompt and tool selection before enabling it for live chat.
-
Deploy the agent and chat with it, e.g.: "Look at my watchlist, check recent news sentiment, and place a small paper trade if you find a good opportunity." Watch the trade land on the dashboard from step 5, and in your Alpaca paper-trading dashboard too.
mcp_server/anddashboard/intentionally duplicatealpaca_broker.pyrather than sharing a package, because each Databricks App deploys independently from its own folder with its ownapp.yaml/requirements.txt- there's no shared Python package install step across Databricks Apps. If you prefer a single shared package, publishalpaca_broker.pyto a private PyPI index or wheel and add it to bothrequirements.txtfiles instead of duplicating.place_tradesubmits real orders against your real Alpaca paper account - fills use real market prices, but no real money moves. Never pointalpaca_broker.pyat live-trading keys for this lab.- The legacy
paper_broker.py+lakebase.pyLakebase-simulated engine is still present in both folders for reference, in case you want to compare a fully local simulation against Alpaca's real paper-trading fills, or fall back to it if you don't want to create an Alpaca account.