Add example: discover available MCP tools via SystemMessage init#1038
Open
ZainAli24 wants to merge 1 commit into
Open
Add example: discover available MCP tools via SystemMessage init#1038ZainAli24 wants to merge 1 commit into
ZainAli24 wants to merge 1 commit into
Conversation
Adds examples/mcp_discover_available_tools.py to show how to inspect which MCP servers connected and which tools are available at session start. The SDK emits a SystemMessage(subtype="init") at the beginning of every query. Its data dict contains mcp_servers (with per-server status) and tools (the full list of callable tools, including MCP tools named mcp__<server>__<tool>). The example also documents a common pitfall: SystemMessage has no .type attribute, so type-checking via message.type raises AttributeError. The correct pattern is isinstance(message, SystemMessage). This fills the gap in the MCP docs page (https://code.claude.com/docs/en/agent-sdk/mcp#discover-available-tools) which currently shows only a TypeScript snippet for this pattern. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The MCP documentation page at
https://code.claude.com/docs/en/agent-sdk/mcp#discover-available-tools
shows only a TypeScript code snippet for discovering available MCP tools.
There is no Python equivalent — all other sections on that page offer a
Python / TypeScript toggle, but this section does not.
Following the TypeScript pattern directly in Python leads to a runtime
error because
SystemMessagedoes not expose a.typeattribute:Solution
This PR adds
examples/mcp_discover_available_tools.py, a self-containedexample that shows the correct Python idiom for inspecting MCP server
status and available tools at session start.
What the example covers
ClaudeAgentOptions.mcp_servers)SystemMessage(subtype="init")event usingisinstance()message.data["mcp_servers"]to check per-server connectionstatus (
"connected","pending","failed","needs-auth")message.data["tools"]to list only MCP tools(
mcp__<server>__<tool>naming convention)Key correctness note documented in the example
Testing
Run the example directly (requires a valid Claude Code session):
Expected output: