Pythonコードインタープリタエージェントツール
Databricks 、 AIエージェントがエージェントによって記述されたPythonコード、ユーザーによって提供された Python コード、またはコードベースから取得した Python コードを動的に実行できるようにする、 Unity Catalog機能であるsystem.ai.python_execを提供します。 これはデフォルトで利用可能であり、SQLクエリで直接使用できます。
SQL
SELECT python_exec("""
import random
numbers = [random.random() for _ in range(10)]
print(numbers)
""")
エージェント ツールの詳細については、 「AI エージェント ツール」を参照してください。
エージェントにコードインタープリタを追加する
エージェントにpython_execを追加するには、 system.ai Unity Catalogスキーマの管理対象MCPサーバーに接続します。 コードインタープリタは、 https://<workspace-hostname>/api/2.0/mcp/functions/system/ai/python_execで事前設定済みの MCP ツールとして利用できます。
- OpenAI Agents SDK (Apps)
- LangGraph (Apps)
- Model Serving
Python
from agents import Agent, Runner
from databricks.sdk import WorkspaceClient
from databricks_openai.agents import McpServer
# WorkspaceClient picks up credentials from the environment (Databricks Apps, notebook, CLI)
workspace_client = WorkspaceClient()
host = workspace_client.config.host
# The context manager manages the MCP connection lifecycle and ensures cleanup on exit.
# from_uc_function constructs the endpoint URL from UC identifiers and wires in auth
# from workspace_client, avoiding hardcoded URLs and manual token handling.
async with McpServer.from_uc_function(
catalog="system",
schema="ai",
function_name="python_exec",
workspace_client=workspace_client,
name="code-interpreter",
) as code_interpreter:
agent = Agent(
name="Coding agent",
instructions="You are a helpful coding assistant. Use the python_exec tool to run code.",
model="databricks-claude-sonnet-4-5",
mcp_servers=[code_interpreter],
)
result = await Runner.run(agent, "Calculate the first 10 Fibonacci numbers")
print(result.final_output)
アプリにdatabricks.yml関数へのアクセス権限を付与します。
YAML
resources:
apps:
my_agent_app:
resources:
- name: 'python_exec'
uc_securable:
securable_full_name: 'system.ai.python_exec'
securable_type: 'FUNCTION'
permission: 'EXECUTE'
Python
from databricks.sdk import WorkspaceClient
from databricks_langchain import ChatDatabricks, DatabricksMCPServer, DatabricksMultiServerMCPClient
from langgraph.prebuilt import create_react_agent
workspace_client = WorkspaceClient()
host = workspace_client.config.host
# DatabricksMultiServerMCPClient provides a unified get_tools() interface across
# multiple MCP servers, making it easy to add more servers later without refactoring.
mcp_client = DatabricksMultiServerMCPClient([
DatabricksMCPServer(
name="code-interpreter",
url=f"{host}/api/2.0/mcp/functions/system/ai/python_exec",
workspace_client=workspace_client,
),
])
async with mcp_client:
tools = await mcp_client.get_tools()
agent = create_react_agent(
ChatDatabricks(endpoint="databricks-claude-sonnet-4-5"),
tools=tools,
)
result = await agent.ainvoke(
{"messages": [{"role": "user", "content": "Calculate the first 10 Fibonacci numbers"}]}
)
# LangGraph returns the full conversation history; the last message is the agent's final response
print(result["messages"][-1].content)
アプリにdatabricks.yml関数へのアクセス権限を付与します。
YAML
resources:
apps:
my_agent_app:
resources:
- name: 'python_exec'
uc_securable:
securable_full_name: 'system.ai.python_exec'
securable_type: 'FUNCTION'
permission: 'EXECUTE'
Python
from databricks.sdk import WorkspaceClient
from databricks_mcp import DatabricksMCPClient
import mlflow
workspace_client = WorkspaceClient()
host = workspace_client.config.host
mcp_client = DatabricksMCPClient(
server_url=f"{host}/api/2.0/mcp/functions/system/ai/python_exec",
workspace_client=workspace_client,
)
tools = mcp_client.list_tools()
# get_databricks_resources() extracts the UC permissions the agent needs at runtime.
# Passing these to log_model lets Model Serving grant access automatically at deployment,
# without requiring manual permission configuration.
mlflow.pyfunc.log_model(
"agent",
python_model=my_agent,
resources=mcp_client.get_databricks_resources(),
)
エージェントをデプロイするには、 「生成AIアプリケーション用のエージェントのデプロイ (モデルサービング)」を参照してください。 MCP を使用したエージェントのログ記録の詳細については、 Databricks管理の MCP サーバーの使用を参照してください。
次のステップ
- AIエージェントツール- エージェント向けのその他のツールタイプをご覧ください。
- Unity Catalog関数を使用してAIエージェントツールを作成する- Unity Catalog関数を使用してカスタムツールを作成します。
- DatabricksのマネージドMCPサーバーを使用する- DatabricksのマネージドMCPサーバーの詳細については、こちらをご覧ください。