Skip to main content

Connect an MCP server

This recipe connects to an MCP server for AI applications using GitHub as an example and the Unity Catalog HTTP connection for secure and governed access.

Code snippets

OAuth User to Machine Per User (On-behalf-of-user)

app.py
from databricks.sdk import WorkspaceClient
from databricks.sdk.service.serving import ExternalFunctionRequestHttpMethod
import json
from flask import request

token = request.headers.get("x-forwarded-access-token")
w = WorkspaceClient(token=token, auth_type="pat")


def init_mcp_session(w: WorkspaceClient, connection_name: str):
init_payload = {
"jsonrpc": "2.0",
"id": "init-1",
"method": "initialize",
"params": {}
}
response = w.serving_endpoints.http_request(
conn=connection_name,
method=ExternalFunctionRequestHttpMethod.POST,
path="/",
json=init_payload,
)
return response.headers.get("mcp-session-id")


connection_name = "github_u2m_connection"
http_method = ExternalFunctionRequestHttpMethod.POST
path = "/"
headers = {"Content-Type": "application/json"}
payload = {"jsonrpc": "2.0", "id": "list-1", "method": "tools/list"}

session_id = init_mcp_session(w, connection_name)
headers["Mcp-Session-Id"] = session_id

response = w.serving_endpoints.http_request(
conn=connection_name,
method=http_method,
path=path,
headers=headers,
json=payload,
)
print(response.json())

Bearer token

app.py
from databricks.sdk import WorkspaceClient
from databricks.sdk.service.serving import ExternalFunctionRequestHttpMethod


w = WorkspaceClient()

response = w.serving_endpoints.http_request(
conn="github_u2m_connection",
method=ExternalFunctionRequestHttpMethod.GET,
path="/traffic/views",
headers={"Accept": "application/vnd.github+json"},
json={
"jsonrpc": "2.0",
"id": "init-1",
"method": "initialize",
"params": {}
},
)

print(response.json())

Resources

Permissions

Your app service principal needs the following permissions:

  • USE CONNECTION permission on the HTTP Connection

When using OAuth User to Machine Per User (On-behalf-of-user), you need to configure User authorization by adding the Unity Catalog connection or other scopes.

Dependencies

requirements.txt
databricks-sdk
dash
uvicorn
mcp[cli]