Add MCP tools
List MCP server tools and feed them into a Yolk agent loop.
Use this when your app lets users configure remote or local MCP servers.
List tools
import { Effect } from 'effect'
import { FetchHttpClient } from 'effect/http'
import type { McpRemoteServerConfig } from '@yolk-sdk/mcp/client'
import { listRemoteMcpServerTools } from '@yolk-sdk/mcp/client'
const server = {
name: 'docs',
type: 'remote',
url: 'https://example.com/mcp'
} satisfies McpRemoteServerConfig
const mcpTools = await Effect.runPromise(
listRemoteMcpServerTools(server).pipe(Effect.provide(FetchHttpClient.layer))
)
const toolDefs = mcpTools.map(tool => tool.def)
const toolByName = new Map(mcpTools.map(tool => [tool.def.name, tool]))Pass toolDefs into run or runRuntime, or register them as tools (below).
Register them as tools
Each listed tool declares ToolDef.outputSchema { value, files }
(ADR 0013), so
its registration sets decodeOutput: mcpToolOutputDecoder: resolveTools checks every result
with it (and fails with output_validation_required without it).
import { ToolError } from '@yolk-sdk/agent/loop'
import type { ToolModule, ToolRegistration } from '@yolk-sdk/agent/tools'
import {
callRemoteMcpServerTool,
mcpToolOutputDecoder,
type McpResolvedTool
} from '@yolk-sdk/mcp/client'
type HostContext = { readonly userId: string } // your app's context
const registration = (resolved: McpResolvedTool): ToolRegistration<HostContext> => ({
def: resolved.def,
access: 'read', // host policy, for example from `resolved.annotations`
decodeOutput: mcpToolOutputDecoder,
execute: ({ call }) =>
callRemoteMcpServerTool({
config: server,
mcpToolName: resolved.mcpToolName,
toolCallId: call.id,
params: call.params
}).pipe(
Effect.provide(FetchHttpClient.layer),
Effect.mapError(
error => new ToolError({ tool: call.name, message: error.message, cause: 'execution' })
)
)
})
const mcpModule: ToolModule<HostContext> = { id: 'mcp-docs', tools: mcpTools.map(registration) }What a call returns
A successful call's structuredContent is { value, files }, and code mode scripts resolve to it:
valueis the server'sstructuredContent(checked against the listedoutputSchema), else the text of its text blocks and embedded text resources (resourcewithtext), in order, parsed once when it is one JSON object or array, else the text,nullwithout text.resource_linkblocks never enter it.filesare its blobs, images and audio asFileRef(orFileUnavailablewhen no store is configured, the file is too large, or storing failed).
The model sees the server's content: a result without blobs, images or audio exactly as before; otherwise images and audio stay viewable media exactly as the server sent them (their inline data reaches providers that accept media), each followed by its file label once stored, and blob resources become file labels, so a blob's base64 never reaches the model (Move files between tools).
Files are stored when the call runs. Inside a registration, resolveTools stores them through its
files option. A call made anywhere else (your own ToolExecutor, a route resuming
resumeRemoteMcpServerToolCall, a direct callLocalMcpServerTool) passes
fileCapture: makeToolFileCapture({ store }) from @yolk-sdk/agent/tools; without it every file
is FileUnavailable (no file store) and the blob is lost (MCP blobs cannot be fetched again).
Local stdio calls list the tools first in the same session and check structuredContent against
the listed outputSchema before trusting it; a listing that fails or lacks the tool runs the call
without that check.
Finish calls that ask for input
A server can pause a tools/call with a form (MCP form elicitation over 2026-07-28 multi
round-trip requests, see Confirm tool calls with form elicitation).
In an agent session the registration stores the pause and returns an input-required result; a
person answers the form through the input review interaction, which resumes the remote call once
(Finish calls that paused for input).
Fragment continuing the registration above: pauseStore (session-scoped pause storage with an
atomic claim) is host-owned.
import * as Schema from 'effect/Schema'
import { InteractionValidationError, makeInputRequiredToolResult } from '@yolk-sdk/agent/protocol'
import {
inputReviewToolName,
makeInputReviewTool,
type InputPause,
type InputReviewHost
} from '@yolk-sdk/agent/tools'
import {
callRemoteMcpServerToolResumable,
McpElicitationResponses,
toMcpInputResponses,
type McpInputRequired
} from '@yolk-sdk/mcp/client'
// One stored pause per paused call: the remote pause and the call it resumes.
type McpPause = InputPause & {
readonly inputRequired: McpInputRequired
readonly toolCallId: string
readonly params: unknown
}
const registration = (resolved: McpResolvedTool): ToolRegistration<HostContext> => ({
def: resolved.def,
access: 'write', // host policy
decodeOutput: mcpToolOutputDecoder,
execute: ({ call }) =>
callRemoteMcpServerToolResumable({
config: server,
mcpToolName: resolved.mcpToolName,
toolCallId: call.id,
params: call.params,
options: { elicitation: { form: true } }
}).pipe(
Effect.flatMap(outcome =>
outcome._tag === 'Complete'
? Effect.succeed(outcome.result)
: pauseStore.save({ call, inputRequired: outcome.inputRequired }).pipe(
Effect.map(pauseId =>
makeInputRequiredToolResult({
toolCallId: call.id,
content: `${server.name} asks for input before ${call.name} can finish.`,
reviewToolName: inputReviewToolName,
pauseIds: [pauseId]
})
)
)
),
Effect.provide(FetchHttpClient.layer),
Effect.mapError(
error => new ToolError({ tool: call.name, message: error.message, cause: 'execution' })
)
)
})
const decodeAnswer = Schema.decodeUnknownEffect(McpElicitationResponses)
const mcpPauses: InputReviewHost<McpPause> = {
load: pauseIds => pauseStore.loadWaiting(pauseIds), // in this session, still waiting, in order
// One answer per pause: its responses keyed by form `key`, checked against the forms.
validateAnswer: ({ pause, answer }) =>
decodeAnswer(answer).pipe(
Effect.flatMap(responses =>
toMcpInputResponses({ server: server.name, inputRequired: pause.inputRequired, responses })
),
Effect.asVoid,
Effect.mapError(error => new InteractionValidationError({ message: error.message }))
),
// Claim, then resumeRemoteMcpServerToolCall, then record (below).
resume: input => pauseStore.resumeOnce(input),
recorded: input => pauseStore.recorded(input)
}
const reviewInput = makeInputReviewTool<HostContext, McpPause>({ host: mcpPauses })resume claims the pause for submissionId before sending, then calls
resumeRemoteMcpServerToolCall({ config, mcpToolName, toolCallId, params, options, inputRequired, responses })
with the stored call and the person's responses, records the resolution, and returns it:
CompletebecomesInputPauseResolution.Completed({ result }).InputRequired(the server asked again): store the new pause and returnInputPauseResolution.InputRequired({ pauseIds: [newPauseId] }).- A failure checked before sending (
McpInputRequiredErrorinvalid_responsesormismatched_resume), or a refusal before sending (the target changed, or the pause was already answered), isNotSent({ message }). - Any other failure after the claim is
Unknown({ message }): the call may have run. Never send a claimed pause again;recordedreports what was recorded after a crash.
The review action provides the resolution's file capture (resolveTools files), so a resume run
in resume's Effect stores its blobs like a registration's; a resume run elsewhere passes
fileCapture.
Host responsibilities
Persist server config, credentials, auth headers, and allow/deny policy in your app. Keep local stdio behind Node-only hosts via @yolk-sdk/mcp/client/node.