feat: expose session context to MCP tool handlers via get_tool_context()#824
Open
Nik-Reddy wants to merge 1 commit intoanthropics:mainfrom
Open
feat: expose session context to MCP tool handlers via get_tool_context()#824Nik-Reddy wants to merge 1 commit intoanthropics:mainfrom
Nik-Reddy wants to merge 1 commit intoanthropics:mainfrom
Conversation
…ntext() Tools registered with @tool can now call get_tool_context() to access session_id, transcript_path, and conversation history during execution. Context is best-effort — depends on hook callbacks having fired before the tool call to provide session metadata. Fixes anthropics#823
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.
Fixes #823
I've been building tool handlers that need to know things about the current session — like reading back conversation history to avoid repeating work, or just knowing the session ID for logging purposes. Right now there's no way to get that info from inside an
@toolfunction.This adds a
ToolContextdataclass and aget_tool_context()function that tool handlers can call to grab session metadata (session_id, transcript_path, cwd, etc.) and optionally read back conversation history.How it works
The SDK already receives session metadata through hook callbacks (
PreToolUse,PostToolUse) before atools/callrequest comes in. This PR captures that metadata into a contextvar, so by the time the tool handler runs, the context is already there.From inside a tool handler:
`python
from claude_agent_sdk import get_tool_context
@tool
def my_tool(args):
ctx = get_tool_context()
if ctx:
history = ctx.get_conversation_history()
# do something with prior messages
`
get_conversation_history()is an explicit method (not a property) since it does file I/O to parse the JSONL transcript — wanted to make that cost obvious to the caller.What's changed
types.py— newToolContextdataclass withget_conversation_history()_internal/_tool_context.py— contextvar module for storing/retrieving context_internal/query.py— captures session metadata from hook inputs, sets the contextvar before MCP tool dispatch, resets it after__init__.py— exportsget_tool_contextandToolContexttests/test_tool_context.py— 16 tests covering the happy path, missing context, file I/O edge cases, contextvar isolationDesign notes
get_tool_context()returnsNoneif no hooks fired before the tool call. Felt like the right tradeoff vs. raising an error.All existing tests still pass (460 total).