McpToolset

Connects to an MCP Server and exposes the server's MCP tools to an agent as ADK BaseTools.

McpToolset manages the lifecycle of the connection to a single MCP server and lazily fetches the server's tool list on first use. The instance can then be passed directly to an LlmAgent's toolsets.

Instances are created via McpToolset.McpToolsetConfig.toToolset, for example:

val toolset =
McpToolset.McpToolsetConfig(
stdioConnectionParams =
McpConnectionParameters.Stdio(
serverParameters =
ServerParameters.builder("npx")
.args("-y", "@modelcontextprotocol/server-filesystem")
.build()
),
toolFilter = ToolFilter.allowList("read_file", "list_directory"),
)
.toToolset()

The constructor is internal; user code should use McpToolset.McpToolsetConfig.toToolset instead. Portable Streamable HTTP on JVM and Android uses RemoteMcpToolsetConfig.

A configured headerProvider is invoked before tool discovery and again before each tool call (matching ADK Python). An IllegalArgumentException from that provider or from a ToolFilter.Predicate during getTools is wrapped in McpToolLoadingException.

Types

Link copied to clipboard
object Companion
Link copied to clipboard
data class McpToolsetConfig(val stdioConnectionParams: McpConnectionParameters.Stdio? = null, val sseConnectionParams: McpConnectionParameters.Sse? = null, val streamableHttpConnectionParams: McpConnectionParameters.StreamableHttp? = null, val toolFilter: ToolFilter? = null, val useMcpResources: Boolean = false, val maxMcpResourceLength: Int = DEFAULT_MAX_MCP_RESOURCE_LENGTH)

Configuration for an McpToolset, used to construct one via toToolset.

Functions

Link copied to clipboard
open override fun close()

Performs cleanup and releases resources held by the toolset.

Link copied to clipboard
open suspend override fun getTools(readonlyContext: ReadonlyContext? = null): List<BaseTool>

Discovers tools advertised by the MCP server.

Link copied to clipboard
open suspend fun processLlmRequest(toolContext: ToolContext, llmRequest: LlmRequest): LlmRequest

Allows the toolset to process the LLM request.