Skip to main content

Use MCP tools in a Python agent

Connect to an MCP server, discover its tools, and run a Python agent that uses them. Use the URL from a Databricks-provided MCP, your registered MCP, or your server on Databricks Apps.

To use MCPs from Claude Code, Codex, or another coding agent, choose your client in Supported coding agents. For other assistants and MCP clients, see Other MCP clients.

For an Agent Bricks CLI project, add MCP tools with the CLI. The examples below show how to connect from your own Python code.

Prerequisites​

  • Python 3.12 on your computer.
  • Your server's MCP URL. If someone shared an MCP with you, they must grant you access. Complete the provider login if it uses per-user OAuth. For a server on Databricks Apps, you need CAN USE on the app.
  • Access to a Databricks model endpoint that supports tool calling. The example uses databricks-claude-sonnet-4-5. Replace it with an endpoint available in your workspace.

To use system.ai.dbsql, system.ai.sandbox, or system.ai.web_search, an account admin must enable the Unity Gateway beta from the account console Previews page. See Manage account previews.

For identity choices and connectivity requirements, see Authentication and network access.

Step 1: Install and sign in​

  1. If you haven't installed the Databricks CLI, run this command on macOS or Linux:

    Bash
    curl -fsSL https://raw.githubusercontent.com/databricks/setup-cli/main/install.sh | sh

    For Windows or other installation methods, see Install the Databricks CLI.

  2. Sign in to your workspace:

    Bash
    databricks auth login --host https://<workspace-hostname> --profile DEFAULT
  3. Install the Python libraries:

    Bash
    pip install --upgrade databricks-mcp databricks-sdk "mcp>=1.24,<2"

The examples use MCP Python 1.x, which is compatible with the agent frameworks below.

Step 2: Connect to your server​

Save the following code as mcp_agent.py. Replace <mcp-server-url> with your server's URL:

  • Databricks-provided or registered MCP: https://<workspace-hostname>/ai-gateway/mcp-services/<catalog>.<schema>.<service>.
  • Server on Databricks Apps: Copy the app URL from its overview page and append /mcp.
Python
from databricks_mcp import DatabricksMCPClient
from databricks.sdk import WorkspaceClient

workspace_client = WorkspaceClient()
server_url = "<mcp-server-url>"
mcp_client = DatabricksMCPClient(
server_url=server_url,
workspace_client=workspace_client,
)

tools = mcp_client.list_tools()
for tool in tools:
print(tool.name, tool.description, tool.inputSchema, sep="\n")

Run the script:

Bash
python mcp_agent.py

You should see your server's tools, with descriptions and input schemas. Choose a read-only task that one of these tools supports for the next step. If the list is empty or the connection fails, see MCP authentication and networking.

Use another server​

To find MCPs you can access in a catalog and schema, run:

Bash
databricks ai-gateway list-mcp-services --parent schemas/system.ai

Replace system.ai with your <catalog>.<schema> to list registered MCPs. The CLI handles pagination.

Step 3: Run an agent with these tools​

Choose your framework, install its package, and append its Python example to mcp_agent.py. Each example uses the same server_url and workspace sign-in from step 2.

The example converts MCP content blocks to the model's chat message format with convert_to_openai_messages.

Bash
pip install --upgrade databricks-langchain langgraph
Python
import asyncio
from databricks_langchain import (
ChatDatabricks,
DatabricksMCPServer,
DatabricksMultiServerMCPClient,
)
from langchain_core.messages import convert_to_openai_messages
from langgraph.prebuilt import create_react_agent

async def main():
client = DatabricksMultiServerMCPClient([
DatabricksMCPServer(
name="my-mcp-server",
url=server_url,
workspace_client=workspace_client,
),
])
agent = create_react_agent(
ChatDatabricks(endpoint="databricks-claude-sonnet-4-5"),
tools=await client.get_tools(),
prompt=lambda state: convert_to_openai_messages(state["messages"]),
)
task = input("Ask the agent to use a tool: ")
result = await agent.ainvoke({
"messages": [{"role": "user", "content": task}],
})
for message in result["messages"]:
print(message)

asyncio.run(main())

Deployment notebook (optional)

For Model Serving deployment, adapt this notebook to use your server URL:

LangGraph MCP tool-calling agent

Run python mcp_agent.py again. When prompted, ask for the read-only task you chose, including any required inputs. For example, if your server has a ticket search tool, ask it to find open tickets in a specific project.

Check the printed conversation for a tool call, its result, and the agent's answer. An answer without a tool call doesn't confirm that the MCP server was used.

Call a tool directly to troubleshoot​

Use the tool name and input schema printed in step 2. This example prompts for the name and arguments so it works with your server's tools. Run it after the connection code in step 2:

Python
import json

tool_name = input("Read-only tool name: ")
arguments = json.loads(input("Tool arguments as a JSON object: "))
result = mcp_client.call_tool(tool_name, arguments)
print(result)

Check that the result has no tool error and contains the data you expected.

Discover tool names and input schemas with list_tools() before calling a tool. Result formats vary by tool:

  • If structuredContent is present, use that structured result directly. A tool can describe its shape with outputSchema.
  • Otherwise, inspect the content blocks. Parse a text block as JSON only if the tool returns JSON. MCP also supports plain text and other content types.
  • Check isError and inspect a sample response before relying on particular output fields.

Deploy and share when you're ready​

The local example runs as you. When you deploy the agent on Databricks Apps, choose the identity it uses: the app's service principal for shared access, or the calling user for per-user access.

For Databricks-provided or registered MCPs:

For legacy workspace servers or servers hosted on Databricks Apps, grant access to the underlying resources or app. See Agent authentication.

Additional resources​