anthropic.tool()

Give Anthropic’s tool runner access to an existing Console session.

Usage

Source

anthropic.tool(console, **kwargs)

The runner calls Console’s send tool and returns its output to Claude before requesting the next response. Keep the connection open for the whole conversation so later tool calls can use variables from earlier cells. A synchronous client produces a synchronous tool; an async client produces an async tool for AsyncAnthropic.

This adapter returns text, with placeholders for images. For plots and other image output, use tools(), which opens its own connection and preserves MCP content blocks.

Parameters

console: MCPConsole | AsyncMCPConsole

A connected MCPConsole or AsyncMCPConsole.

**kwargs: Any
Options passed to Anthropic’s beta_tool or beta_async_tool, such as defer_loading.

Returns

Any
A tool to include in client.beta.messages.tool_runner(tools=...).

Examples

Install with pip install 'mcp-console[anthropic]'. Set ANTHROPIC_API_KEY and set ANTHROPIC_MODEL to the Claude model ID you want to use. This script prints the model’s final answer:

import os
import mcp_console
from anthropic import Anthropic
from mcp_console import MCPConsole

with Anthropic() as client, MCPConsole() as console:
    runner = client.beta.messages.tool_runner(
        model=os.environ["ANTHROPIC_MODEL"],
        max_tokens=4096,
        tools=[mcp_console.anthropic.tool(console)],
        messages=[
            {
                "role": "user",
                "content": "Use Python in Console to calculate the mean "
                "and sample standard deviation of [12, 15, 18, 20, 25].",
            }
        ],
    )
    message = runner.until_done()
    for block in message.content:
        if block.type == "text":
            print(block.text)