From 129feebc2be19c91dcda8329ee1052f22fb7ca91 Mon Sep 17 00:00:00 2001 From: Twisha Bansal <58483338+twishabansal@users.noreply.github.com> Date: Mon, 11 Aug 2025 19:01:14 +0530 Subject: [PATCH] docs: add js samples to docsite (#1127) --- docs/en/how-to/deploy_toolbox.md | 33 +++++++++++----- docs/en/resources/authServices/_index.md | 49 ++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 10 deletions(-) diff --git a/docs/en/how-to/deploy_toolbox.md b/docs/en/how-to/deploy_toolbox.md index 091612f402..11abfe8844 100644 --- a/docs/en/how-to/deploy_toolbox.md +++ b/docs/en/how-to/deploy_toolbox.md @@ -151,18 +151,31 @@ You can connect to Toolbox Cloud Run instances directly through the SDK. 1. Import and initialize the toolbox client with the URL retrieved above: - ```python - from toolbox_core import ToolboxClient, auth_methods + {{< tabpane persist=header >}} +{{< tab header="Python" lang="python" >}} +from toolbox_core import ToolboxClient, auth_methods - # Replace with the Cloud Run service URL generated in the previous step. - URL = "https://cloud-run-url.app" +# Replace with the Cloud Run service URL generated in the previous step. +URL = "https://cloud-run-url.app" - auth_token_provider = auth_methods.aget_google_id_token(URL) # can also use sync method +auth_token_provider = auth_methods.aget_google_id_token(URL) # can also use sync method + +async with ToolboxClient( + URL, + client_headers={"Authorization": auth_token_provider}, +) as toolbox: +{{< /tab >}} +{{< tab header="Javascript" lang="javascript" >}} +import { ToolboxClient } from '@toolbox-sdk/core'; +import {getGoogleIdToken} from '@toolbox-sdk/core/auth' + +// Replace with the Cloud Run service URL generated in the previous step. +const URL = 'http://127.0.0.1:5000'; +const authTokenProvider = () => getGoogleIdToken(URL); + +const client = new ToolboxClient(URL, null, {"Authorization": authTokenProvider}); +{{< /tab >}} +{{< /tabpane >}} - async with ToolboxClient( - URL, - client_headers={"Authorization": auth_token_provider}, - ) as toolbox: - ``` Now, you can use this client to connect to the deployed Cloud Run instance! diff --git a/docs/en/resources/authServices/_index.md b/docs/en/resources/authServices/_index.md index 94cfe8fea8..516a5b1aa1 100644 --- a/docs/en/resources/authServices/_index.md +++ b/docs/en/resources/authServices/_index.md @@ -68,6 +68,10 @@ parameter when loading tools, or the `add_auth_token_getter`() / ### Specifying tokens during load +#### Python + +Use the [Python SDK](https://github.com/googleapis/mcp-toolbox-sdk-python/tree/main). + {{< tabpane persist=header >}} {{< tab header="Core" lang="Python" >}} import asyncio @@ -135,8 +139,33 @@ if **name** == "**main**": asyncio.run(main()){{< /tab >}} {{< /tabpane >}} +#### Javascript/Typescript + +Use the [JS SDK](https://github.com/googleapis/mcp-toolbox-sdk-js/tree/main). + +```javascript +import { ToolboxClient } from '@toolbox-sdk/core'; + +async function getAuthToken() { + // ... Logic to retrieve ID token (e.g., from local storage, OAuth flow) + // This example just returns a placeholder. Replace with your actual token retrieval. + return "YOUR_ID_TOKEN" // Placeholder +} + +const URL = 'http://127.0.0.1:5000'; +let client = new ToolboxClient(URL); +const authTool = await client.loadTool("my-tool", {"my_auth_app_1": getAuthToken}); +const result = await authTool({param:"value"}); +console.log(result); +print(result) +``` + + ### Specifying tokens for existing tools +#### Python +Use the [Python SDK](https://github.com/googleapis/mcp-toolbox-sdk-python/tree/main). + {{< tabpane persist=header >}} {{< tab header="Core" lang="Python" >}} tools = await toolbox.load_toolset() @@ -182,4 +211,24 @@ authorized_tool = tools[0].add_auth_token_getters({ {{< /tab >}} {{< /tabpane >}} +#### Javascript/Typescript + +Use the [JS SDK](https://github.com/googleapis/mcp-toolbox-sdk-js/tree/main). + +```javascript +const URL = 'http://127.0.0.1:5000'; +let client = new ToolboxClient(URL); +let tool = await client.loadTool("my-tool") + +// for a single token +const authorizedTool = tool.addAuthTokenGetter("my_auth", get_auth_token) + +// OR, if multiple tokens are needed +const multiAuthTool = tool.addAuthTokenGetters({ + "my_auth_1": getAuthToken1, + "my_auth_2": getAuthToken2, +}) + +``` + ## Kinds of Auth Services