Skip to content

feat: add context parameter to prompt handlers#857

Closed
hashwnath wants to merge 2 commits into
ArcadeAI:mainfrom
hashwnath:feat/707-prompt-handler-context
Closed

feat: add context parameter to prompt handlers#857
hashwnath wants to merge 2 commits into
ArcadeAI:mainfrom
hashwnath:feat/707-prompt-handler-context

Conversation

@hashwnath

@hashwnath hashwnath commented Jun 1, 2026

Copy link
Copy Markdown

Summary

Closes #707

  • Add optional context parameter to prompt handlers for SDK parity with TypeScript
  • Use inspect.signature introspection to detect handler arity, supporting both old-style (args) and new-style (context, args) signatures for backward compatibility
  • Pass the per-request Context (from get_current_model_context()) through PromptManager.get_prompt to PromptHandler.get_messages
  • Update type annotations from Callable[[dict[str, str]], list[PromptMessage]] to Callable[..., list[PromptMessage]] across prompt manager, server, and MCPApp

New prompt handler pattern

# New style - receives context (context first, matching tools)
async def greeting(context: Context, args: dict[str, str]) -> list[PromptMessage]:
    await context.log.info("Generating greeting")
    return [PromptMessage(role="user", content=f"Hello {args['name']}")]

# Old style - still works (backward compatible)
async def greeting(args: dict[str, str]) -> list[PromptMessage]:
    return [PromptMessage(role="user", content=f"Hello {args['name']}")]

Test plan

  • New-style handler with context receives the context object
  • Old-style handler without context still works when context is provided (backward compat)
  • Old-style handler works when no context is provided at all
  • Default handler (no user handler) works with context
  • Synchronous handler with context works
  • Signature introspection correctly detects 1-param vs 2-param handlers
  • All 14 existing prompt tests pass unchanged

Generated with Claude Code


Note

Low Risk
Additive API with signature-based dispatch; existing single-argument handlers are unchanged, and context is only injected on the prompts/get path.

Overview
Adds optional per-request Context to MCP prompt handlers so Python SDK behavior aligns with tools and the TypeScript SDK.

PromptHandler uses inspect.signature at registration time: handlers with two required positional parameters (after self) are invoked as (context, args); single-parameter handlers still receive only args. PromptManager.get_prompt and MCPServer._handle_get_prompt pass context from get_current_model_context() into that path. Handler type hints widen to Callable[..., list[PromptMessage]] on the manager, server, and MCPApp prompts API.

Tests cover context-aware async/sync handlers, backward compatibility when context is omitted or ignored, default handlers, and the introspection helper.

Reviewed by Cursor Bugbot for commit 6599bc2. Bugbot is set up for automated code reviews on this repo. Configure here.

Use signature introspection to support both old-style (args-only) and
new-style (context, args) prompt handler signatures, matching the
pattern used by tool handlers. Context is sourced from the existing
per-request ContextVar and passed through to prompt handlers that
accept it.

Closes ArcadeAI#707

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Comment thread libs/arcade-mcp-server/arcade_mcp_server/managers/prompt.py
Comment thread libs/arcade-mcp-server/arcade_mcp_server/managers/prompt.py
… heuristic

Fixes false positives where handlers with keyword-only params or extra
defaulted params were incorrectly identified as context-accepting.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 6599bc2. Configure here.

result = await self._prompt_manager.get_prompt(
message.params.name,
message.params.arguments if hasattr(message.params, "arguments") else None,
context=context,

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Context not forwarded through Prompts.get() call path

Medium Severity

The _prompt_manager.get_prompt call in context.py:557 (Prompts.get()) was not updated to pass the context parameter, even though self._ctx (the Context object) is readily available. Prompt handlers that accept context will receive None when invoked through this path (e.g., a tool calling a prompt via context.prompts.get()), while the same handler called via the MCP protocol (_handle_get_prompt) correctly receives context.

Additional Locations (1)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 6599bc2. Configure here.

@github-actions

Copy link
Copy Markdown

This pull request has been automatically marked as stale because it has had no activity for 14 days. It will be closed in 14 days if no further activity occurs. If this is still relevant, please leave a comment or remove the stale label.

@github-actions github-actions Bot added the stale label Jun 16, 2026
@github-actions

github-actions Bot commented Jul 1, 2026

Copy link
Copy Markdown

This pull request has been closed due to inactivity. Feel free to reopen it if it is still relevant.

@github-actions github-actions Bot closed this Jul 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Python: Add context parameter to prompt handlers for SDK parity

1 participant