Skip to content

MCP Support in Workspace Client #981

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions databricks/sdk/__init__.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions databricks/sdk/service/mcp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from mcp.client.auth import OAuthClientProvider, TokenStorage
from mcp.shared.auth import OAuthToken


class DatabricksTokenStorage(TokenStorage):
def __init__(self, config):
self.config = config

async def get_tokens(self) -> OAuthToken | None:
headers = self.config.authenticate()
token = headers["Authorization"].split("Bearer ")[1]
return OAuthToken(access_token=token, expires_in=60)


class MCP:
def __init__(self, config):
self._config = config
self.databricks_token_storage = DatabricksTokenStorage(config)

def get_oauth_provider(self):
return OAuthClientProvider(
server_url="",
client_metadata=None,
storage=self.databricks_token_storage,
redirect_handler=None,
callback_handler=None,
)
5 changes: 5 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ dev = [
'langchain-openai; python_version > "3.7"',
"httpx",
"build", # some integration tests depend on the databricks-sdk-py wheel
"mcp>=1.9.1",
"pytest-asyncio"
]
notebook = [
"ipython>=8,<10",
Expand All @@ -63,6 +65,9 @@ openai = [
'langchain-openai; python_version > "3.7"',
"httpx",
]
mcp = [
"mcp>=1.9.1"
]

[tool.setuptools.dynamic]
version = { attr = "databricks.sdk.version.__version__" }
Expand Down
20 changes: 20 additions & 0 deletions tests/test_mcp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import time

import httpx
import pytest


@pytest.mark.asyncio
async def test_mcp_oauth_provider(monkeypatch):
monkeypatch.setattr(time, "time", lambda: 100)
from databricks.sdk import WorkspaceClient

monkeypatch.setenv("DATABRICKS_HOST", "test_host")
monkeypatch.setenv("DATABRICKS_TOKEN", "test_token")

w = WorkspaceClient()
mcp_oauth_provider = w.mcp.get_oauth_provider()

request = httpx.Request("GET", "https://example.com")
response = await anext(mcp_oauth_provider.async_auth_flow(request))
assert response.headers["Authorization"] == "Bearer test_token"
Loading