Skip to content

Add example: discover available MCP tools via SystemMessage init#1038

Open
ZainAli24 wants to merge 1 commit into
anthropics:mainfrom
ZainAli24:feat/add-mcp-discover-tools-example
Open

Add example: discover available MCP tools via SystemMessage init#1038
ZainAli24 wants to merge 1 commit into
anthropics:mainfrom
ZainAli24:feat/add-mcp-discover-tools-example

Conversation

@ZainAli24

Copy link
Copy Markdown

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 SystemMessage does not expose a .type attribute:

# This raises AttributeError: 'SystemMessage' object has no attribute 'type'
if message.type == "system" and message.subtype == "init":
    ...

Solution

This PR adds examples/mcp_discover_available_tools.py, a self-contained
example that shows the correct Python idiom for inspecting MCP server
status and available tools at session start.

What the example covers

  • Connecting to an HTTP MCP server (ClaudeAgentOptions.mcp_servers)
  • Detecting the SystemMessage(subtype="init") event using isinstance()
  • Reading message.data["mcp_servers"] to check per-server connection
    status ("connected", "pending", "failed", "needs-auth")
  • Filtering message.data["tools"] to list only MCP tools
    (mcp__<server>__<tool> naming convention)

Key correctness note documented in the example

# WRONG — raises AttributeError
if message.type == "system" and message.subtype == "init":

# CORRECT — use isinstance() to check message type
if isinstance(message, SystemMessage) and message.subtype == "init":

Testing

Run the example directly (requires a valid Claude Code session):

python examples/mcp_discover_available_tools.py

Expected output:

MCP server connection status:
  ✓ claude-code-docs: connected

Available MCP tools (N):
  - mcp__claude-code-docs__<tool_name>
  ...

Result:
<Claude's response listing available tools>

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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant