@@ -12,6 +12,60 @@ class MCPToolContext(typing.Dict[str, typing.Any]):
1212 pass
1313
1414
15+ class PromptInvocationContext :
16+ """Context object for MCP prompt triggers.
17+
18+ Provides structured access to prompt invocation details including
19+ the prompt name, arguments, session information, and transport metadata.
20+
21+ Attributes:
22+ name: The name of the prompt being invoked
23+ arguments: Dictionary of argument name to value (all values are strings)
24+ sessionid: Optional session ID for the invocation
25+ transport: Optional transport information dictionary
26+
27+ Example:
28+ >>> context = PromptInvocationContext(data)
29+ >>> code = context.arguments.get("code", "")
30+ >>> language = context.arguments.get("language", "python")
31+ """
32+
33+ def __init__ (self , data : typing .Union [dict , str ]):
34+ """Initialize from trigger data received from the host.
35+
36+ Args:
37+ data: Either a dict containing the context or a JSON string
38+ """
39+ if isinstance (data , str ):
40+ import json
41+ data = json .loads (data )
42+
43+ self ._data = data if isinstance (data , dict ) else {}
44+
45+ @property
46+ def name (self ) -> str :
47+ """The name of the prompt being invoked."""
48+ return self ._data .get ('name' , '' )
49+
50+ @property
51+ def arguments (self ) -> typing .Dict [str , str ]:
52+ """Dictionary of prompt arguments (all values are strings)."""
53+ return self ._data .get ('arguments' , {})
54+
55+ @property
56+ def sessionid (self ) -> typing .Optional [str ]:
57+ """Optional session ID for the invocation."""
58+ return self ._data .get ('sessionid' )
59+
60+ @property
61+ def transport (self ) -> typing .Optional [typing .Dict [str , typing .Any ]]:
62+ """Optional transport information."""
63+ return self ._data .get ('transport' )
64+
65+ def __repr__ (self ) -> str :
66+ return f"PromptInvocationContext(name={ self .name !r} , arguments={ self .arguments !r} )"
67+
68+
1569class _MCPToolTriggerConverter (meta .InConverter , binding = 'mcpToolTrigger' ,
1670 trigger = True ):
1771
@@ -104,3 +158,53 @@ def encode(cls, obj: typing.Any, *, expected_type: typing.Optional[type] = None)
104158 else :
105159 # Convert other types to string
106160 return meta .Datum (type = 'string' , value = str (obj ))
161+
162+
163+ class _MCPPromptTriggerConverter (meta .InConverter , binding = 'mcpPromptTrigger' ,
164+ trigger = True ):
165+
166+ @classmethod
167+ def check_input_type_annotation (cls , pytype : type ) -> bool :
168+ return issubclass (pytype , (str , dict , bytes , PromptInvocationContext ))
169+
170+ @classmethod
171+ def has_implicit_output (cls ) -> bool :
172+ return True
173+
174+ @classmethod
175+ def decode (cls , data : meta .Datum , * , trigger_metadata ):
176+ """
177+ Decode incoming MCP prompt request data.
178+ Returns a PromptInvocationContext object.
179+ """
180+ # Handle different data types appropriately
181+ if data .type == 'json' :
182+ # If it's already parsed JSON, create context from dict
183+ return PromptInvocationContext (data .value )
184+ elif data .type == 'string' :
185+ # If it's a JSON string, create context from string
186+ return PromptInvocationContext (data .value )
187+ elif data .type == 'bytes' :
188+ # Decode bytes to string then create context
189+ return PromptInvocationContext (data .value .decode ('utf-8' ))
190+ else :
191+ # Fallback to python_value for other types
192+ value = data .python_value if hasattr (data , 'python_value' ) else data .value
193+ return PromptInvocationContext (value )
194+
195+ @classmethod
196+ def encode (cls , obj : typing .Any , * , expected_type : typing .Optional [type ] = None ):
197+ """
198+ Encode the return value from MCP prompt functions.
199+ MCP prompts typically return string responses that the host wraps as messages.
200+ Can also return a JSON-serialized GetPromptResult for advanced scenarios.
201+ """
202+ if obj is None :
203+ return meta .Datum (type = 'string' , value = '' )
204+ elif isinstance (obj , str ):
205+ return meta .Datum (type = 'string' , value = obj )
206+ elif isinstance (obj , (bytes , bytearray )):
207+ return meta .Datum (type = 'bytes' , value = bytes (obj ))
208+ else :
209+ # Convert other types to string
210+ return meta .Datum (type = 'string' , value = str (obj ))
0 commit comments