Skip to main content

Use MCP servers in Custom Agents

Preview

This feature is in Public Preview.

Connect your agent code to any MCP server on Databricks: Databricks-managed servers, external MCP servers registered as MCP Services, and custom servers hosted as Databricks apps. All of them expose the same MCP interface, so the agent code is the same. What differs is the server URL and how you authenticate.

The databricks-mcp Python library handles authentication to Databricks MCP servers, so the same client code works across all three server types.

Get your server URL

Set up the MCP server first, then use its URL in the following examples:

Server type

URL pattern

Set up

Managed

https://<workspace-hostname>/api/2.0/mcp/<service>/<path>

Available managed servers

External (MCP Service)

https://<workspace-hostname>/ai-gateway/mcp-services/<catalog>.<schema>.<mcp-service>

Connect agents to tools with MCP Services

Custom

https://<app-url>/mcp

Host your own MCP server

Server type

URL pattern

Set up

Managed

https://<workspace-hostname>/api/2.0/mcp/<service>/<path>

Available managed servers

External (MCP Service)

https://<workspace-hostname>/ai-gateway/mcp-services/<catalog>.<schema>.<mcp-service>

Connect agents to tools with MCP Services

Custom

https://<app-url>/mcp

Host your own MCP server

Discover available MCP servers and tools

Before writing agent code, find which servers and tools you can use. Do not hardcode server names, tool names, or argument shapes from memory; confirm them from the workspace.

  • Browse servers in the workspace. Go to AI Gateway > MCPs to see the MCP servers available to you. Databricks provides ready-to-use built-in servers: managed MCP servers for your own Unity Catalog data and functions (Genie spaces, Vector Search indexes, and Unity Catalog functions), and built-in system.ai MCP Services for third-party SaaS tools such as Slack, GitHub, Google Drive, Google Calendar, Gmail, and Microsoft 365.

  • List MCP Services programmatically. List the MCP Services in any catalog and schema with the Unity Catalog REST API. For example, the built-in services:

    Bash
    databricks api get "/api/2.1/unity-catalog/mcp-services?parent=schemas/system.ai&page_size=100"

    Replace system.ai with your own <catalog>.<schema> to find services you registered. page_size is capped at 100, and the response includes a next_page_token when more services exist. To enumerate every service in a schema, repeat the request with page_token=<next_page_token> until the response returns no token:

    Bash
    token=""
    while :; do
    page=$(databricks api get "/api/2.1/unity-catalog/mcp-services?parent=schemas/system.ai&page_size=100&page_token=$token")
    echo "$page"
    token=$(echo "$page" | jq -r '.next_page_token // empty')
    [ -z "$token" ] && break
    done
  • List a server's tools from code. Point a DatabricksMCPClient at any server URL and call list_tools() to get each tool's name, description, and input schema at runtime, as shown in Connect and list tools. This is the reliable way to learn a server's exact tools and arguments.

Set up your environment

  1. Use OAuth to authenticate to your workspace:

    Bash
    databricks auth login --host https://<workspace-hostname>
  2. When prompted, enter a profile name and note it for later. The default profile name is DEFAULT.

  3. Verify you have a local environment with Python 3.12 or above, then install dependencies:

    Bash
    pip install -U "mcp>=1.9" "databricks-sdk[openai]" "mlflow>=3.1.0" "databricks-agents>=1.0.0" "databricks-mcp"

    The agent-framework examples below need their own SDK. Add openai-agents databricks-openai for the OpenAI Agents SDK, or databricks-langchain langgraph for LangGraph.

Connect and list tools

Create a DatabricksMCPClient with the server URL and list its tools. The same client works for managed, external (MCP Service), and custom server URLs:

Python
from databricks_mcp import DatabricksMCPClient
from databricks.sdk import WorkspaceClient

workspace_client = WorkspaceClient(profile="DEFAULT")
host = workspace_client.config.host

# Use a managed, MCP Service, or custom server URL:
mcp_server_url = f"{host}/api/2.0/mcp/functions/system/ai"

mcp_client = DatabricksMCPClient(server_url=mcp_server_url, workspace_client=workspace_client)
tools = mcp_client.list_tools()
print(f"Available tools: {[t.name for t in tools]}")

To call a tool directly:

Python
result = mcp_client.call_tool("system__ai__python_exec", {"code": "print('Hello, world!')"})
print(result.content)
note

Serverless compute must be enabled in your workspace to run managed system.ai tools.

Authenticate

Select the authentication method that matches where your agent runs. For an external MCP Service, the caller must also have EXECUTE on the service. AI Gateway enforces this permission on every call.

Authenticate to your workspace with OAuth (see Set up your environment) and pass the profile to the client:

Python
workspace_client = WorkspaceClient(profile="DEFAULT")
mcp_client = DatabricksMCPClient(server_url=mcp_server_url, workspace_client=workspace_client)

Build an agent

Use an agent framework to turn the MCP server's tools into an agent. Point the framework at the server URL and pass your authenticated WorkspaceClient.

Python
import asyncio
from agents import Agent, Runner
from databricks.sdk import WorkspaceClient
from databricks_openai.agents import McpServer


async def main():
workspace_client = WorkspaceClient()
host = workspace_client.config.host

async with McpServer(
url=f"{host}/ai-gateway/mcp-services/main.default.github_mcp",
name="github-mcp",
workspace_client=workspace_client,
) as mcp_server:
agent = Agent(
name="Local agent",
instructions="You are a helpful assistant with access to external services.",
model="databricks-claude-sonnet-4-5",
mcp_servers=[mcp_server],
)
result = await Runner.run(agent, "List my open GitHub pull requests.")
print(result.final_output)


asyncio.run(main())

Example notebooks

The following notebooks show how to build LangGraph and OpenAI agents that call MCP tools across managed, external, and custom MCP servers:

LangGraph MCP tool-calling agent

OpenAI MCP tool-calling agent

Agents SDK MCP tool-calling agent

Deploy your agent

Databricks recommends deploying agents on Databricks Apps, which lets you fully manage the agent code, server configuration, and git-based versioning. Alternatively, deploy on Model Serving.

Whichever you select, grant the agent access to every resource its MCP servers depend on. For example, CAN_RUN on a Genie Agent or SELECT on an AI Search index.

Declare each resource your agent uses, including the resources behind every MCP server, under resources.apps.<app>.resources in databricks.yml, then deploy the bundle to grant the app's Databricks service principal access. For example, for an agent that uses the managed Genie and AI Search servers:

YAML
resources:
apps:
my_agent_app:
name: 'my-agent-app'
source_code_path: ./
resources:
- name: 'llm'
serving_endpoint:
name: 'databricks-claude-sonnet-4-5'
permission: 'CAN_QUERY'
- name: 'genie_space'
genie_space:
space_id: '<genie-space-id>'
permission: 'CAN_RUN'
- name: 'vector_index'
uc_securable:
securable_full_name: '<catalog>.<schema>.<index-name>'
securable_type: 'TABLE'
permission: 'SELECT'
Bash
databricks bundle deploy
databricks bundle run my_agent_app

For the full authoring and deployment workflow, see Author an agent and deploy it on Databricks Apps. For all resource types and permission values, see Authentication for agents.

note

Start from an agent template: it provides the MLflow AgentServer entrypoint (run with uv run start-app), the get_user_workspace_client() on-behalf-of helper, and a databricks.yml. Pin the interpreter with requires-python = ">=3.12,<3.13" and commit uv.lock so the Databricks Apps build image does not resolve a newer Python (for example, 3.14) that lacks prebuilt wheels for some agent dependencies. For an MCP Service, also grant the caller access out of band (see Enable per-user access (on-behalf-of-user access)); bundle validate passes without it.

Next steps