adk-web icon indicating copy to clipboard operation
adk-web copied to clipboard

Allow user-provided API key at runtime with OpenAPI tools

Open justinli34 opened this issue 8 months ago • 2 comments

Is it possible for tools created with the OpenAPIToolset to use API keys provided by the user in chat?

Currently it seems that the auth_credential can only be set upon initialization. When using the adk api_server, this means credentials are shared between users since there is only one root_agent per app.

It would be helpful to support user-provided API keys at runtime, so that each user’s credentials can be used only within their own session. Could this feature be added, or is there a recommended pattern for handling per-user credentials in this scenario?

Thanks

justinli34 avatar May 22 '25 15:05 justinli34

I don't know why ADK recommend setting environment key as kinda default way.

The ADK wrap the litellm and reexpose the client. You can set the key via litellm

Image
import os
from dotenv import load_dotenv
from google.adk.models.lite_llm import LiteLlm
from google.adk.agents.llm_agent import LlmAgent
from google.adk.tools.mcp_tool.mcp_toolset import (
    MCPToolset,
    SseServerParams,
)

load_dotenv()

root_agent = LlmAgent(
    model=LiteLlm(
        model="openai/gpt-4.1-nano",
        api_key=os.getenv("MY_OPENAI_API_KEY"), # this is for demo purpose, replace with user-provided API keys at runtime
    ),
    name="Agent",
    instruction="""My Agent Instructions
    """,
    tools=[
        MCPToolset(
            connection_params=SseServerParams(
                url="http://localhost:8001/sse",
            )
        )
    ],
)

kimyu-ng avatar May 22 '25 17:05 kimyu-ng

thank you for your question @justinli34 , we do have such support on the server but not on UI. you can set auth scheme to api key but set None auth credential, then agent turn will end with a long running function call (adk_request_credential) , your agent client needs to send back a function response to this function call with api key values similar to what we did for sending back for oauth response here : https://github.com/google/adk-web/blob/9960cf2d0b1b27abe99e140516dc27dd9ca9a218/src/app/components/chat/chat.component.ts#L590

seanzhou1023 avatar May 23 '25 18:05 seanzhou1023