Skip to content

Commit 4b95af5

Browse files
committed
create MCPToolContext class
1 parent 3ba01e8 commit 4b95af5

File tree

5 files changed

+15
-37
lines changed

5 files changed

+15
-37
lines changed

azure/functions/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
DecoratorApi, DataType, AuthLevel,
1111
Cardinality, AccessRights, HttpMethod,
1212
AsgiFunctionApp, WsgiFunctionApp,
13-
ExternalHttpFunctionApp, BlobSource, MCPToolContext)
13+
ExternalHttpFunctionApp, BlobSource)
1414
from ._durable_functions import OrchestrationContext, EntityContext
1515
from .decorators.function_app import (FunctionRegister, TriggerApi,
1616
BindingApi, SettingsApi)
@@ -19,6 +19,7 @@
1919
from ._http_wsgi import WsgiMiddleware
2020
from ._http_asgi import AsgiMiddleware
2121
from .kafka import KafkaEvent, KafkaConverter, KafkaTriggerConverter
22+
from .mcp import MCPToolContext
2223
from .meta import get_binding_registry
2324
from ._queue import QueueMessage
2425
from ._servicebus import ServiceBusMessage
@@ -32,6 +33,7 @@
3233
from . import eventhub # NoQA
3334
from . import http # NoQA
3435
from . import kafka # NoQA
36+
from . import mcp # NoQA
3537
from . import queue # NoQA
3638
from . import servicebus # NoQA
3739
from . import timer # NoQA

azure/functions/decorators/__init__.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from .function_app import FunctionApp, Function, DecoratorApi, DataType, \
55
AuthLevel, Blueprint, ExternalHttpFunctionApp, AsgiFunctionApp, \
66
WsgiFunctionApp, FunctionRegister, TriggerApi, BindingApi, \
7-
SettingsApi, BlobSource, MCPToolContext
7+
SettingsApi, BlobSource
88
from .http import HttpMethod
99

1010
__all__ = [
@@ -24,6 +24,5 @@
2424
'Cardinality',
2525
'AccessRights',
2626
'HttpMethod',
27-
'BlobSource',
28-
'MCPToolContext'
27+
'BlobSource'
2928
]

azure/functions/decorators/function_app.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,11 @@
4343
AssistantQueryInput, AssistantPostInput, InputType, EmbeddingsInput, \
4444
semantic_search_system_prompt, \
4545
SemanticSearchInput, EmbeddingsStoreOutput
46-
from .mcp import MCPToolTrigger, MCPToolContext, _TYPE_MAPPING, _extract_type_and_description
46+
from .mcp import MCPToolTrigger, _TYPE_MAPPING, _extract_type_and_description
4747
from .retry_policy import RetryPolicy
4848
from .function_name import FunctionName
4949
from .warmup import WarmUpTrigger
50+
from ..mcp import MCPToolContext
5051
from .._http_asgi import AsgiMiddleware
5152
from .._http_wsgi import WsgiMiddleware, Context
5253
from azure.functions.decorators.mysql import MySqlInput, MySqlOutput, \
@@ -502,7 +503,7 @@ def decorator(fb: FunctionBuilder) -> FunctionBuilder:
502503
"description": param_desc,
503504
})
504505

505-
tool_properties_json = json.dumps(tool_properties)\
506+
tool_properties_json = json.dumps(tool_properties)
506507

507508
bound_params = [
508509
inspect.Parameter(name, inspect.Parameter.POSITIONAL_OR_KEYWORD)

azure/functions/decorators/mcp.py

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
MCP_TOOL_TRIGGER
77
)
88
from azure.functions.decorators.core import Trigger, DataType
9-
from azure.functions.decorators.function_app import FunctionBuilder
109

1110
# Mapping Python types to MCP property types
1211
_TYPE_MAPPING = {
@@ -34,10 +33,6 @@ def __init__(self,
3433
self.tool_properties = tool_properties
3534
super().__init__(name=name, data_type=data_type)
3635

37-
# MCP-specific context object
38-
class MCPToolContext(Dict[str, Any]):
39-
"""Injected context object for MCP tool triggers."""
40-
pass
4136

4237
# Helper to extract actual type and description from Annotated types
4338
def _extract_type_and_description(param_name: str, type_hint: Any) -> Tuple[Any, str]:
@@ -48,28 +43,3 @@ def _extract_type_and_description(param_name: str, type_hint: Any) -> Tuple[Any,
4843
param_description = next((a for a in args[1:] if isinstance(a, str)), f"The {param_name} parameter.")
4944
return actual_type, param_description
5045
return type_hint, f"The {param_name} parameter."
51-
52-
def _get_user_function(target_func):
53-
"""
54-
Unwraps decorated or builder-wrapped functions to find the original
55-
user-defined function (the one starting with 'def' or 'async def').
56-
"""
57-
logging.info("HELLO FROM THE SDK")
58-
# Case 1: It's a FunctionBuilder object
59-
if isinstance(target_func, FunctionBuilder):
60-
# Access the internal user function
61-
try:
62-
return target_func._function.get_user_function()
63-
except AttributeError:
64-
pass
65-
66-
# Case 2: It's already the user-defined function
67-
if callable(target_func) and hasattr(target_func, "__name__"):
68-
return target_func
69-
70-
# Case 3: It might be a partially wrapped callable
71-
if hasattr(target_func, "__wrapped__"):
72-
return _get_user_function(target_func.__wrapped__)
73-
74-
# Default fallback
75-
return target_func

azure/functions/mcp.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,18 @@
33
from . import meta
44

55

6+
# MCP-specific context object
7+
class MCPToolContext(typing.Dict[str, typing.Any]):
8+
"""Injected context object for MCP tool triggers."""
9+
pass
10+
11+
612
class MCPToolTriggerConverter(meta.InConverter, binding='mcpToolTrigger',
713
trigger=True):
814

915
@classmethod
1016
def check_input_type_annotation(cls, pytype: type) -> bool:
11-
return issubclass(pytype, (str, dict, bytes))
17+
return issubclass(pytype, (str, dict, bytes, MCPToolContext))
1218

1319
@classmethod
1420
def has_implicit_output(cls) -> bool:

0 commit comments

Comments
 (0)