Custom Tools and MCP Servers
Every tool you add to an Agent SDK agent is an MCP tool. Either you write it in-process with `tool()` / `@tool` and `createSdkMcpServer()` / `create_sdk_mcp_server()`, or you connect a stdio, HTTP or SSE server through `mcpServers`. Interviewers probe four things: the result shape and how errors reach Claude, the `mcp__<server>__<tool>` name that every allow rule needs, reading connection status from the init message, and scaling with tool search.
Key points
- 1
Defining a tool takes a name, a description, an input schema and an async handler. TypeScript uses
tool(name, desc, zodShape, handler, extras?), with.describe()on fields. Python uses@tool(name, desc, schema, annotations=...)with a dict like{'lat': float}(every key required),Annotated[float, 'desc'], a TypedDict or a full JSON Schema. Wrap the tools withcreateSdkMcpServer({ name, version, tools })and pass the result under a key inmcpServers. - 2
SDK servers (
type: 'sdk') run inside your process, with no extra deployable and direct access to your app's state and credentials. The other configs are stdio (command,args,env),type: 'http'(streamable HTTP;.mcp.jsonalso acceptsstreamable-http) andtype: 'sse', each withurlandheaders. - 3
Handlers return
{ content: [...] }, withtext,image(raw base64dataplusmimeType, no URL),audio,resourceorresource_linkblocks, plus optionalstructuredContentandisError. Python usesis_error; the Python decorator forwards onlycontentandis_error, soisErrorandstructuredContentare silently dropped there. - 4
A handler that throws does not end the query: the in-process server returns an error result with the raw message and the loop continues. Catch the error and return
isError: truewith an actionable message when the raw text is not enough. - 5
Tool names are
mcp__<server key>__<tool>, where the key is the one used inmcpServersand.or spaces become_. Plugin servers becomemcp__plugin_<plugin>_<server>__<tool>. Allow rules need the full name or an anchored glob such asmcp__crm__*; unanchoredmcp__*in an allow list approves nothing. - 6
acceptEditsnever approves MCP tools. UseallowedToolsrather thanbypassPermissions.tools: []removes every built-in while leaving MCP tools available.strictMcpConfig/strict_mcp_configloads only the servers you pass, ignoring.mcp.json, user settings, plugins and claude.ai connectors. - 7
.mcp.jsonloads only when theprojectsetting source is on (it is by default). It expands${VAR}and${VAR:-default}incommand,args,env,urlandheaders, but credential variables such asANTHROPIC_API_KEYandNPM_TOKENread as empty in a remote server'surlandheaders. - 8
The init message's
mcp_serversgives each server'sstatus:connected,failed,needs-auth,pendingordisabled. Itstoolsarray lists themcp__tools available at that moment. Connection failures never throw. For live status usemcpServerStatus()(Python:ClaudeSDKClient.get_mcp_status()); to retry usereconnectMcpServer()/reconnect_mcp_server(), and to switch a server off or on usetoggleMcpServer(). - 9
The SDK runs no interactive OAuth. A server with no usable sign-in reports
needs-auth; complete OAuth in your app and passAuthorization: Bearer <token>inheaders. - 10
Tool search is on by default and also defers SDK tools: Claude sees names and loads up to five full schemas per search.
ENABLE_TOOL_SEARCHtakes unset/true,auto(activates at 10% of the context window),auto:Norfalse. In TypeScript,alwaysLoad: trueontool()extras or on a server keeps schemas upfront, andsearchHintimproves matching. - 11
There are two timeouts.
MCP_TIMEOUT(30 seconds by default) limits connection.MCP_TOOL_TIMEOUTlimits each call, and a per-servertimeoutin milliseconds overrides it (values below 1000 are ignored; progress notifications do not extend it). Results above 25,000 tokens (MAX_MCP_OUTPUT_TOKENS) or 50,000 characters are saved to a file and Claude gets the path. - 12
TypeScript
setMcpServers()replaces only the servers it added plus in-process SDK servers. Stdio, HTTP and SSE servers from the options, settings-file servers and plugin servers keep running.onElicitationanswers server input requests; without it (and without a hook) they are declined.
Common traps
Putting the bare tool name (
get_weather) inallowedTools: it never matches, and in aquery()with no callback every call lands inpermission_denials.Returning
'isError': Truefrom a Python handler: the decorator reads onlyis_error, so the failure reaches Claude as a success.Treating
pendingin the init message as a failure: it can mean still connecting, served from the tool-list cache, or a deadline at the boundary. Check forfailedandneeds-auth.Expecting
readOnlyHintordestructiveHintto enforce anything: for SDK tools onlyreadOnlyHintchanges behaviour (parallel batching), and none of them stops the handler.Confusing
MCP_TIMEOUT(connection) withMCP_TOOL_TIMEOUTand per-servertimeout(call duration), or passing seconds where milliseconds are expected.Letting the model supply security context, such as a tenant ID, as a tool argument instead of closing over it in a per-request in-process server.
Moving a Python MCP server that uses elicitation, sampling or progress in-process: those server-to-client messages are not forwarded yet.
Read the source
Further reading
Suggest a resourceNo extra reading for Custom Tools and MCP Servers yet. Found a guide, video or cheat sheet that made it click? Share it and help the next learner.
Test yourself on Custom Tools and MCP Servers
Ten questions, with the answer and explanation after each one.