mcpTools

suspend fun mcpTools(server: Client, prefix: String = "", timeout: Duration? = null, filter: (Tool) -> Boolean = { true }): List<Tool>

Returns the tools of the Model Context Protocol server that server is connected to, as tools of an agent. Many services publish such a server, so an agent gets their tools without a Tool written by hand for each.

// The app connects, with the Kotlin SDK of MCP. Over HTTP this works on every platform.
val server = HttpClient().mcpStreamableHttp("https://mcp.example.com/mcp")

val agent = toolAgent(model, tools = mcpTools(server) + ownTools)
agent.invoke(AgentState("Which issues are open?"))

server.close()

The function asks the server for all of its tools once, page by page. Each tool keeps the name, the description and the input schema the server gave it, and running it calls the server. Call this function before you build the graph, and close server when the app is done with the agent: the connection belongs to the app. A server that changes its tools later is not followed: call the function again and build the agent again.

What a tool returns to the model:

  • The text parts of the result, joined. An image, a sound or a file becomes one line that says what it was, such as [image: image/png].

  • A result the server marks as an error is an error result for the model, like a tool of the app that throws. So is a call that failed or got no answer in time. The run goes on, and the model can react.

To let an agent use only some tools, pick them with filter, which gets what the server says about each:

val readOnly = mcpTools(server) { tool -> tool.annotations?.readOnlyHint == true }

To approve the calls by hand, run the graph with interruptBefore = setOf("tools"), as for the tools of the app.

A model accepts letters, digits, _ and - in the name of a tool. Any other character of a name, such as the dot of repo.search, is given to the model as _, and the server is called with the real name.

Parameters

server

a client of the MCP SDK that is connected to the server.

prefix

put before the name of every tool, for example "github_", so that the tools of two servers, or of a server and the app, cannot have the same name.

timeout

how long one call of a tool may take. null leaves the 60 seconds of the SDK.

filter

returns true for the tools to keep. It gets each tool as the server describes it.

Throws

when the server cannot be asked for its tools.

if a name is longer than 64 characters with its prefix, or two tools end up with the same name.