One of the biggest challenges with LLMs is bridging the gap between static knowledge and real-world actions. MCP solves this by providing a standard way for models to connect with external tools and data sources.
The Model Context Protocol (MCP) is an open standard that allows Large Language Models (LLMs) to connect with external tools, data sources, and APIs in a structured way. Instead of relying only on what the model knows, MCP gives the LLM controlled access to real-time information and custom functionality.
In this article, we’ll walk through a step-by-step guide to build a custom MCP server and deploy it as a Databricks app. Once deployed, we’ll test this MCP server in the Databricks playground by attaching it as a tool to the LLM model.
For demonstration, we’ll build simple tools—one to fetch NSE stock market data and another to show how an agent can interact with Databricks (e.g., creating tables or running SQL queries). Similarly, multiple other tools can be developed to seamlessly integrate with Databricks, such as tools for workflow creation and more.
Note: The tools and resources provided related to stock market data are intended solely for educational purposes and should not be interpreted as financial or investment advice.
We’ll cover the following steps:
git clone https://github.com/databrickslabs/mcp.gitAlthough these tools or Databricks resources can be invoked directly via API or SDK in agent code, wrapping them in an MCP server as Databricks apps provides additional benefits. MCP standardizes the interface, making tools automatically discoverable across downstream agentic frameworks, ensures consistent responses, and eliminates the need for each team to write and maintain separate code.
Note: In addition to MCP, Databricks also offers the option to create tools using Unity Catalog function tools and Agent Code tools. For more information, please refer to https://docs.databricks.com/aws/en/generative-ai/agent-framework/agent-tool#choose-your-tool-approac...
Adding the code snippet for each tools
import yfinance as yf
import requests
import pandas as pd
from io import StringIO
import databricks.sql
import os
from databricks.sdk.core import Config
from mcp.server.fastmcp import FastMCP
# create MCP server
mcp = FastMCP("custom MCP server on databricks apps")
# Tool 1
@mcp.tool(
name = "get_stock_info",
description = "Fetch the stock information of the given symbol"
)
def get_stock_info(symbol: str) -> str | None:
"""Fetch market cap, current price, and 52-week range for NSE stock."""
try:
info = yf.Ticker(f"{symbol}.NS").fast_info
return (
f"\nDetails for {symbol}.NS:\n"
f"market_cap = {info['marketCap']}\n"
f"current_price = {info['last_price']} INR\n"
f"year_low = {info['year_low']} INR\n"
f"year_high = {info['year_high']} INR\n"
)
except Exception as e:
print(f"Error fetching {symbol}: {e}")
return None
# Tool 2
@mcp.tool(
name="run_query_on_databricks",
description="Gets the SQL query and execute the query on databricks SQL warehouse"
)
def run_query_on_databricks(sql_query: str):
"""
Executes a SQL query on a Databricks SQL Warehouse and returns the results as a pandas DataFrame.
"""
cfg = Config()
warehouse_id = os.getenv("WAREHOUSE_ID")
host = cfg.host
http_path = f"/sql/1.0/warehouses/{warehouse_id}"
if not all([host, http_path]):
raise ValueError("Missing required Databricks connection environment variables.")
with databricks.sql.connect(server_hostname=host, http_path=http_path, credentials_provider=lambda: cfg.authenticate) as conn, conn.cursor() as cur:
cur.execute(sql_query)
rows = cur.fetchall()
return pd.DataFrame(rows, columns=[c[0] for c in cur.description])
4. Update environment variables in app.yaml
command: ["uv", "run", "custom-server"]
env:
- name: 'WAREHOUSE_ID'
value: '<warehouse-id>'
5. Add required Python libraries in requirements.txt
uv
yfinance
pandas
databricks-sql-connector
python-dotenv
With the above steps, we’ve successfully set up the MCP server in our local IDE. Now its time to do the deployment.
Deploying the MCP server as a Databricks app can be done in just a few steps:
databricks configure # you will be asked to enter host url and the PAT token
databricks auth login --host <workspace-url>databricks apps create mcp-<app_name>DATABRICKS_USERNAME=$(databricks current-user me | jq -r .userName)
databricks sync . "/Users/$DATABRICKS_USERNAME/mcp-custom-server"
databricks apps deploy mcp-<app_name> --source-code-path "/Workspace/Users/$DATABRICKS_USERNAME/mcp-custom-server/custom-server"GRANT ALL ON CATALOG <catalog_name> TO <service_principal>This custom MCP server can be called with LangGraph or any agent flow locally or in databricks notebooks.
Refer to langgraph-mcp-tool-calling-agent - Databricks
Through this walkthrough, we saw how easy it is to extend Databricks with a custom MCP server — from deployment as an App to testing it live in the Playground.
Databricks is making it simple to bring AI Agents and real-world data together in one place. Instead of juggling multiple systems, you can now empower your LLMs to fetch external insights, run SQL on your lakehouse, and interact with enterprise data securely — all within the Databricks platform.
https://docs.databricks.com/aws/en/generative-ai/mcp/custom-mc
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.