Skip to main content

Call MCP tools from a Python agent

Use MCP tools in a custom Python agent built with LangGraph, the OpenAI Agents SDK, or the OpenAI client. The examples show how to sign in, discover tools, and run an agent locally with your own Databricks credentials.

For an app that calls tools on behalf of signed-in users, see Connect your app to MCP tools.

For other ways to connect, see Supported coding agents, Other MCP clients, or Add MCP tools with the Agent Bricks CLI.

Prerequisites​

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, see Supported outbound authentication. For connectivity requirements, see MCP networking.

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.

Use another server​

  • For a built-in MCP, use its full name in the MCP URL, such as system.ai.github.
  • For an existing integration with a workspace MCP server, replace server_url with its workspace endpoint URL.

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 workspace MCP endpoints or servers hosted on Databricks Apps, grant access to the underlying resources or app. See Agent authentication.

Additional resources​