Skip to main content

Use MCP servers in agents

Preview

This feature is in Public Preview.

Connect your agent code to any MCP server on DatabricksDatabricks-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 third-party tools with MCP Services

Custom

https://<app-url>/mcp

Host your own MCP server

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"

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

Open notebook in new tab

OpenAI MCP tool-calling agent

Open notebook in new tab

Agents SDK MCP tool-calling agent

Open notebook in new tab

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 Space 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 AI agent and deploy it on Databricks Apps. For all resource types and permission values, see Authentication for AI agents.

Next steps