forked from microsoft/agent-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_app.py
More file actions
1413 lines (1168 loc) · 58.4 KB
/
Copy path_app.py
File metadata and controls
1413 lines (1168 loc) · 58.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright (c) Microsoft. All rights reserved.
"""AgentFunctionApp - Main application class.
This module provides the AgentFunctionApp class that integrates Microsoft Agent Framework
with Azure Durable Entities, enabling stateful and durable AI agent execution.
"""
from __future__ import annotations
import asyncio
import json
import logging
import re
import uuid
from collections.abc import Callable, Mapping
from copy import deepcopy
from dataclasses import dataclass
from datetime import datetime, timezone
from typing import TYPE_CHECKING, Any, TypeVar, cast
import azure.durable_functions as df
import azure.functions as func
from agent_framework import AgentExecutor, SupportsAgentRun, Workflow, WorkflowEvent
from agent_framework._workflows._runner_context import YieldOutputEventType
from agent_framework_durabletask import (
DEFAULT_MAX_POLL_RETRIES,
DEFAULT_POLL_INTERVAL_SECONDS,
MIMETYPE_APPLICATION_JSON,
MIMETYPE_TEXT_PLAIN,
REQUEST_RESPONSE_FORMAT_JSON,
REQUEST_RESPONSE_FORMAT_TEXT,
THREAD_ID_FIELD,
THREAD_ID_HEADER,
WAIT_FOR_RESPONSE_FIELD,
WAIT_FOR_RESPONSE_HEADER,
AgentResponseCallbackProtocol,
AgentSessionId,
ApiResponseFields,
DurableAgentState,
DurableAIAgent,
RunRequest,
)
from ._context import CapturingRunnerContext
from ._entities import create_agent_entity
from ._errors import IncomingRequestError
from ._orchestration import AgentOrchestrationContextType, AgentTask, AzureFunctionsAgentExecutor
from ._serialization import deserialize_value, serialize_value, strip_pickle_markers
from ._workflow import (
SOURCE_HITL_RESPONSE,
SOURCE_ORCHESTRATOR,
execute_hitl_response_handler,
run_workflow_orchestrator,
)
logger = logging.getLogger("agent_framework.azurefunctions")
EntityHandler = Callable[[df.DurableEntityContext], None]
HandlerT = TypeVar("HandlerT", bound=Callable[..., Any])
def _create_state_snapshot(state: dict[str, Any]) -> dict[str, Any]:
"""Create a deep copy of the deserialized state for later diffing."""
return deepcopy(state)
@dataclass
class AgentMetadata:
"""Metadata for a registered agent.
Attributes:
agent: The agent instance implementing SupportsAgentRun
http_endpoint_enabled: Whether HTTP endpoint is enabled for this agent
mcp_tool_enabled: Whether MCP tool endpoint is enabled for this agent
"""
agent: SupportsAgentRun
http_endpoint_enabled: bool
mcp_tool_enabled: bool
if TYPE_CHECKING:
class DFAppBase:
def __init__(self, http_auth_level: func.AuthLevel = func.AuthLevel.FUNCTION) -> None: ...
def function_name(self, name: str) -> Callable[[HandlerT], HandlerT]: ...
def route(self, route: str, methods: list[str]) -> Callable[[HandlerT], HandlerT]: ...
def durable_client_input(self, client_name: str) -> Callable[[HandlerT], HandlerT]: ...
def entity_trigger(self, context_name: str, entity_name: str) -> Callable[[EntityHandler], EntityHandler]: ...
def orchestration_trigger(self, context_name: str) -> Callable[[HandlerT], HandlerT]: ...
def activity_trigger(self, input_name: str) -> Callable[[HandlerT], HandlerT]: ...
def mcp_tool_trigger(
self,
arg_name: str,
tool_name: str,
description: str,
tool_properties: str,
data_type: func.DataType,
) -> Callable[[HandlerT], HandlerT]: ...
else:
DFAppBase = df.DFApp # type: ignore[assignment]
class AgentFunctionApp(DFAppBase):
"""Main application class for creating durable agent function apps using Durable Entities.
This class uses Durable Entities pattern for agent execution, providing:
- Stateful agent conversations
- Conversation history management
- Signal-based operation invocation
- Better state management than orchestrations
Example:
-------
.. code-block:: python
from agent_framework.azure import AgentFunctionApp
from agent_framework.openai import OpenAIChatCompletionClient
# Create agents with unique names
weather_agent = OpenAIChatCompletionClient(...).as_agent(
name="WeatherAgent",
instructions="You are a helpful weather agent.",
tools=[get_weather],
)
math_agent = OpenAIChatCompletionClient(...).as_agent(
name="MathAgent",
instructions="You are a helpful math assistant.",
tools=[calculate],
)
# Option 1: Pass list of agents during initialization
app = AgentFunctionApp(agents=[weather_agent, math_agent])
# Option 2: Add agents after initialization
app = AgentFunctionApp()
app.add_agent(weather_agent)
app.add_agent(math_agent)
@app.orchestration_trigger(context_name="context")
def my_orchestration(context):
writer = app.get_agent(context, "WeatherAgent")
session = writer.create_session()
forecast_task = writer.run("What's the forecast?", session=session)
forecast = yield forecast_task
return forecast
This creates:
- HTTP trigger endpoint for each agent's requests (if enabled)
- Durable entity for each agent's state management and execution
- Full access to all Azure Functions capabilities
Attributes:
agents: Dictionary of agent name to SupportsAgentRun instance
enable_health_check: Whether health check endpoint is enabled
enable_http_endpoints: Whether HTTP endpoints are created for agents
enable_mcp_tool_trigger: Whether MCP tool triggers are created for agents
max_poll_retries: Maximum polling attempts when waiting for responses
poll_interval_seconds: Delay (seconds) between polling attempts
workflow: Optional Workflow instance for workflow orchestration
"""
_agent_metadata: dict[str, AgentMetadata]
enable_health_check: bool
enable_http_endpoints: bool
enable_mcp_tool_trigger: bool
workflow: Workflow | None
def __init__(
self,
agents: list[SupportsAgentRun] | None = None,
workflow: Workflow | None = None,
http_auth_level: func.AuthLevel = func.AuthLevel.FUNCTION,
enable_health_check: bool = True,
enable_http_endpoints: bool = True,
max_poll_retries: int = DEFAULT_MAX_POLL_RETRIES,
poll_interval_seconds: float = DEFAULT_POLL_INTERVAL_SECONDS,
enable_mcp_tool_trigger: bool = False,
default_callback: AgentResponseCallbackProtocol | None = None,
):
"""Initialize the AgentFunctionApp.
:param agents: List of agent instances to register.
:param workflow: Optional Workflow instance to extract agents from and set up orchestration.
:param http_auth_level: HTTP authentication level (default: ``func.AuthLevel.FUNCTION``).
:param enable_health_check: Enable the built-in health check endpoint (default: ``True``).
:param enable_http_endpoints: Enable HTTP endpoints for agents (default: ``True``).
:param enable_mcp_tool_trigger: Enable MCP tool triggers for agents (default: ``False``).
When enabled, agents will be exposed as MCP tools that can be invoked by MCP-compatible clients.
:param max_poll_retries: Maximum polling attempts when waiting for a response.
Defaults to ``DEFAULT_MAX_POLL_RETRIES``.
:param poll_interval_seconds: Delay in seconds between polling attempts.
Defaults to ``DEFAULT_POLL_INTERVAL_SECONDS``.
:param default_callback: Optional callback invoked for agents without specific callbacks.
:note: If no agents are provided, they can be added later using :meth:`add_agent`.
"""
logger.debug("[AgentFunctionApp] Initializing with Durable Entities...")
# Initialize parent DFApp
super().__init__(http_auth_level=http_auth_level)
# Initialize agent metadata dictionary
self._agent_metadata = {}
self.enable_health_check = enable_health_check
self.enable_http_endpoints = enable_http_endpoints
self.enable_mcp_tool_trigger = enable_mcp_tool_trigger
self.default_callback = default_callback
self.workflow = workflow
try:
retries = int(max_poll_retries)
except (TypeError, ValueError):
retries = DEFAULT_MAX_POLL_RETRIES
self.max_poll_retries = max(1, retries)
try:
interval = float(poll_interval_seconds)
except (TypeError, ValueError):
interval = DEFAULT_POLL_INTERVAL_SECONDS
self.poll_interval_seconds = interval if interval > 0 else DEFAULT_POLL_INTERVAL_SECONDS
# If workflow is provided, extract agents and set up orchestration
if workflow:
if agents is None:
agents = []
logger.debug("[AgentFunctionApp] Extracting agents from workflow")
for executor in workflow.executors.values():
if isinstance(executor, AgentExecutor):
agents.append(executor.agent)
else:
# Setup individual activity for each non-agent executor
self._setup_executor_activity(executor.id)
self._setup_workflow_orchestration()
if agents:
# Register all provided agents
logger.debug(f"[AgentFunctionApp] Registering {len(agents)} agent(s)")
for agent_instance in agents:
self.add_agent(agent_instance)
# Setup health check if enabled
if self.enable_health_check:
self._setup_health_route()
logger.debug("[AgentFunctionApp] Initialization complete")
def _setup_executor_activity(self, executor_id: str) -> None:
"""Register an activity for executing a specific non-agent executor.
Args:
executor_id: The ID of the executor to create an activity for.
"""
activity_name = f"dafx-{executor_id}"
logger.debug(f"[AgentFunctionApp] Registering activity '{activity_name}' for executor '{executor_id}'")
# Capture executor_id in closure
captured_executor_id = executor_id
@self.function_name(activity_name)
@self.activity_trigger(input_name="inputData")
def executor_activity(inputData: str) -> str:
"""Activity to execute a specific non-agent executor.
Note: We use str type annotations instead of dict to work around
Azure Functions worker type validation issues with dict[str, Any].
"""
from agent_framework._workflows._state import State
data_obj = json.loads(inputData)
if not isinstance(data_obj, dict):
raise ValueError("Activity inputData must decode to a JSON object")
data = cast(dict[str, Any], data_obj)
message_data = data.get("message")
shared_state_snapshot = data.get("shared_state_snapshot", {})
source_executor_ids = cast(list[str], data.get("source_executor_ids", [SOURCE_ORCHESTRATOR]))
if not self.workflow:
raise RuntimeError("Workflow not initialized in AgentFunctionApp")
executor = self.workflow.executors.get(captured_executor_id)
if not executor:
raise ValueError(f"Unknown executor: {captured_executor_id}")
# Reconstruct message: strip untrusted pickle/type markers first
# (defense-in-depth), then deserialize_value restores typed objects.
message = deserialize_value(strip_pickle_markers(message_data))
# Check if this is a HITL response message by examining source_executor_ids
is_hitl_response = any(s.startswith(SOURCE_HITL_RESPONSE) for s in source_executor_ids)
async def run() -> dict[str, Any]:
# Create runner context and shared state
runner_context = CapturingRunnerContext()
workflow = self.workflow
def classify_yielded_output(executor_id: str) -> YieldOutputEventType | None:
if workflow is None:
return "output"
if workflow.is_terminal_executor(executor_id):
return "output"
if workflow.is_intermediate_executor(executor_id):
return "intermediate"
return None
runner_context.set_yield_output_classifier(classify_yielded_output)
shared_state = State()
# Deserialize shared state values to reconstruct dataclasses/Pydantic models
deserialized_state: dict[str, Any] = {
str(k): deserialize_value(strip_pickle_markers(v)) for k, v in shared_state_snapshot.items()
}
original_snapshot = _create_state_snapshot(deserialized_state)
shared_state.import_state(deserialized_state)
if is_hitl_response:
# Handle HITL response by calling the executor's @response_handler
if not isinstance(message_data, dict):
raise ValueError("HITL message payload must be a JSON object")
await execute_hitl_response_handler(
executor=executor,
hitl_message=cast(dict[str, Any], message_data),
shared_state=shared_state,
runner_context=runner_context,
)
else:
# Execute using the public execute() method
await executor.execute(
message=message,
source_executor_ids=source_executor_ids,
state=shared_state,
runner_context=runner_context,
)
# Commit pending state changes and export
shared_state.commit()
current_state = shared_state.export_state()
original_keys: set[str] = set(original_snapshot.keys())
current_keys: set[str] = set(current_state.keys())
# Deleted = was in original, not in current
deletes: set[str] = original_keys - current_keys
# Updates = keys in current that are new or have different values
updates: dict[str, Any] = {}
for key in current_keys:
if key not in original_keys or current_state[key] != original_snapshot.get(key):
updates[key] = current_state[key]
# Drain messages and events from runner context
sent_messages = await runner_context.drain_messages()
events = await runner_context.drain_events()
# Extract outputs from WorkflowEvent instances with type='output'
outputs: list[Any] = []
for event in events:
if isinstance(event, WorkflowEvent) and event.type == "output":
outputs.append(serialize_value(event.data))
# Get pending request info events for HITL
pending_request_info_events = await runner_context.get_pending_request_info_events()
# Serialize pending request info events for orchestrator
serialized_pending_requests: list[dict[str, Any]] = []
for _request_id, event in pending_request_info_events.items():
serialized_pending_requests.append({
"request_id": event.request_id,
"source_executor_id": event.source_executor_id,
"data": serialize_value(event.data),
"request_type": f"{type(event.data).__module__}:{type(event.data).__name__}",
"response_type": f"{event.response_type.__module__}:{event.response_type.__name__}"
if event.response_type
else None,
})
# Serialize messages for JSON compatibility
serialized_sent_messages: list[dict[str, Any]] = []
for _source_id, msg_list in sent_messages.items():
for msg in msg_list:
serialized_sent_messages.append({
"message": serialize_value(msg.data),
"target_id": msg.target_id,
"source_id": msg.source_id,
})
serialized_updates = {k: serialize_value(v) for k, v in updates.items()}
return {
"sent_messages": serialized_sent_messages,
"outputs": outputs,
"shared_state_updates": serialized_updates,
"shared_state_deletes": list(deletes),
"pending_request_info_events": serialized_pending_requests,
}
result = asyncio.run(run())
return json.dumps(result)
# Ensure the function is registered (prevents garbage collection)
_ = executor_activity
def _setup_workflow_orchestration(self) -> None:
"""Register the workflow orchestration and related HTTP endpoints."""
@self.orchestration_trigger(context_name="context")
def workflow_orchestrator(context: df.DurableOrchestrationContext) -> Any: # type: ignore[type-arg]
"""Generic orchestrator for running the configured workflow."""
if self.workflow is None:
raise RuntimeError("Workflow not initialized in AgentFunctionApp")
input_data = context.get_input()
# Ensure input is a string for the agent
initial_message = json.dumps(input_data) if isinstance(input_data, (dict, list)) else str(input_data)
# Create local shared state dict for cross-executor state sharing
shared_state: dict[str, Any] = {}
outputs = yield from run_workflow_orchestrator(context, self.workflow, initial_message, shared_state)
# Durable Functions runtime extracts return value from StopIteration
return outputs # noqa: B901
@self.route(route="workflow/run", methods=["POST"])
@self.durable_client_input(client_name="client")
async def start_workflow_orchestration(
req: func.HttpRequest, client: df.DurableOrchestrationClient
) -> func.HttpResponse:
"""HTTP endpoint to start the workflow."""
try:
req_body = req.get_json()
except ValueError:
return self._build_error_response("Invalid JSON body")
instance_id = await client.start_new("workflow_orchestrator", client_input=req_body)
base_url = self._build_base_url(req.url)
status_url = f"{base_url}/api/workflow/status/{instance_id}"
return func.HttpResponse(
json.dumps({
"instanceId": instance_id,
"statusQueryGetUri": status_url,
"respondUri": f"{base_url}/api/workflow/respond/{instance_id}/{{requestId}}",
"message": "Workflow started",
}),
status_code=202,
mimetype="application/json",
)
@self.route(route="workflow/status/{instanceId}", methods=["GET"])
@self.durable_client_input(client_name="client")
async def get_workflow_status(
req: func.HttpRequest, client: df.DurableOrchestrationClient
) -> func.HttpResponse:
"""HTTP endpoint to get workflow status."""
instance_id = req.route_params.get("instanceId")
if not instance_id:
return self._build_error_response("Instance ID is required", status_code=400)
status = await client.get_status(instance_id)
if not status:
return self._build_error_response("Instance not found", status_code=404)
response = {
"instanceId": status.instance_id,
"runtimeStatus": status.runtime_status.name if status.runtime_status else None,
"customStatus": status.custom_status,
"output": status.output,
"error": status.output if status.runtime_status == df.OrchestrationRuntimeStatus.Failed else None,
"createdTime": status.created_time.isoformat() if status.created_time else None,
"lastUpdatedTime": status.last_updated_time.isoformat() if status.last_updated_time else None,
}
# Add pending HITL requests info if available
if (
(custom_status := status.custom_status)
and isinstance(custom_status, dict)
and (pending_requests_dict := custom_status.get("pending_requests")) # type: ignore
and isinstance(pending_requests_dict, dict)
):
base_url = self._build_base_url(req.url)
pending_requests: list[dict[str, Any]] = []
for req_id, req_data in pending_requests_dict.items(): # type: ignore
if not isinstance(req_data, dict):
continue
pending_requests.append({
"requestId": req_id,
"sourceExecutor": req_data.get("source_executor_id"), # type: ignore[reportUnknownMemberType]
"requestData": req_data.get("data"), # type: ignore[reportUnknownMemberType]
"requestType": req_data.get("request_type"), # type: ignore[reportUnknownMemberType]
"responseType": req_data.get("response_type"), # type: ignore[reportUnknownMemberType]
"respondUrl": f"{base_url}/api/workflow/respond/{instance_id}/{req_id}",
})
response["pendingHumanInputRequests"] = pending_requests
return func.HttpResponse(
json.dumps(response, default=str),
status_code=200,
mimetype="application/json",
)
@self.route(route="workflow/respond/{instanceId}/{requestId}", methods=["POST"])
@self.durable_client_input(client_name="client")
async def send_hitl_response(req: func.HttpRequest, client: df.DurableOrchestrationClient) -> func.HttpResponse:
"""HTTP endpoint to send a response to a pending HITL request.
The requestId in the URL corresponds to the request_id from the RequestInfoEvent.
The request body should contain the response data matching the expected response_type.
"""
instance_id = req.route_params.get("instanceId")
request_id = req.route_params.get("requestId")
if not instance_id or not request_id:
return self._build_error_response("Instance ID and Request ID are required.")
try:
response_data = req.get_json()
except ValueError:
return self._build_error_response("Request body must be valid JSON.")
# Sanitize untrusted HTTP input before it reaches pickle.loads().
# See strip_pickle_markers() docstring for details on the attack vector.
response_data = strip_pickle_markers(response_data)
# Send the response as an external event
# The request_id is used as the event name for correlation
await client.raise_event(
instance_id=instance_id,
event_name=request_id,
event_data=response_data,
)
return func.HttpResponse(
json.dumps({
"message": "Response delivered successfully",
"instanceId": instance_id,
"requestId": request_id,
}),
status_code=200,
mimetype="application/json",
)
# Ensure route handlers are registered (prevents unused function warnings)
_ = start_workflow_orchestration
_ = get_workflow_status
_ = send_hitl_response
def _build_status_url(self, request_url: str, instance_id: str) -> str:
"""Build the status URL for a workflow instance."""
base_url = self._build_base_url(request_url)
return f"{base_url}/api/workflow/status/{instance_id}"
def _build_base_url(self, request_url: str) -> str:
"""Extract the base URL from a request URL."""
base_url, _, _ = request_url.partition("/api/")
if not base_url:
base_url = request_url.rstrip("/")
return base_url
@property
def agents(self) -> dict[str, SupportsAgentRun]:
"""Returns dict of agent names to agent instances.
Returns:
Dictionary mapping agent names to their SupportsAgentRun instances.
"""
return {name: metadata.agent for name, metadata in self._agent_metadata.items()}
def add_agent(
self,
agent: SupportsAgentRun,
callback: AgentResponseCallbackProtocol | None = None,
enable_http_endpoint: bool | None = None,
enable_mcp_tool_trigger: bool | None = None,
) -> None:
"""Add an agent to the function app after initialization.
Args:
agent: The Microsoft Agent Framework agent instance (must implement SupportsAgentRun)
The agent must have a 'name' attribute.
callback: Optional callback invoked during agent execution
enable_http_endpoint: Optional flag to enable/disable HTTP endpoint for this agent.
The app level enable_http_endpoints setting will override this setting.
enable_mcp_tool_trigger: Optional flag to enable/disable MCP tool trigger for this agent.
The app level enable_mcp_tool_trigger setting will override this setting.
Raises:
ValueError: If the agent doesn't have a 'name' attribute.
"""
# Get agent name from the agent's name attribute
name = getattr(agent, "name", None)
if name is None:
raise ValueError("Agent does not have a 'name' attribute. All agents must have a 'name' attribute.")
if name in self._agent_metadata:
logger.warning("[AgentFunctionApp] Agent '%s' is already registered, skipping duplicate.", name)
return
effective_enable_http_endpoint = (
self.enable_http_endpoints if enable_http_endpoint is None else self._coerce_to_bool(enable_http_endpoint)
)
effective_enable_mcp_endpoint = (
self.enable_mcp_tool_trigger
if enable_mcp_tool_trigger is None
else self._coerce_to_bool(enable_mcp_tool_trigger)
)
logger.debug(f"[AgentFunctionApp] Adding agent: {name}")
logger.debug(f"[AgentFunctionApp] Route: /api/agents/{name}")
logger.debug(
"[AgentFunctionApp] HTTP endpoint %s for agent '%s'",
"enabled" if effective_enable_http_endpoint else "disabled",
name,
)
logger.debug(
f"[AgentFunctionApp] MCP tool trigger: {'enabled' if effective_enable_mcp_endpoint else 'disabled'}"
)
# Store agent metadata
self._agent_metadata[name] = AgentMetadata(
agent=agent,
http_endpoint_enabled=effective_enable_http_endpoint,
mcp_tool_enabled=effective_enable_mcp_endpoint,
)
effective_callback = callback or self.default_callback
self._setup_agent_functions(
agent, name, effective_callback, effective_enable_http_endpoint, effective_enable_mcp_endpoint
)
logger.debug(f"[AgentFunctionApp] Agent '{name}' added successfully")
def get_agent(
self,
context: AgentOrchestrationContextType,
agent_name: str,
) -> DurableAIAgent[AgentTask]:
"""Return a DurableAIAgent proxy for a registered agent.
Args:
context: Durable Functions orchestration context invoking the agent.
agent_name: Name of the agent registered on this app.
Returns:
DurableAIAgent[AgentTask] wrapper bound to the orchestration context.
Raises:
ValueError: If the requested agent has not been registered.
"""
normalized_name = str(agent_name)
if normalized_name not in self._agent_metadata:
raise ValueError(f"Agent '{normalized_name}' is not registered with this app.")
executor = AzureFunctionsAgentExecutor(context)
return DurableAIAgent(executor, normalized_name)
def _setup_agent_functions(
self,
agent: SupportsAgentRun,
agent_name: str,
callback: AgentResponseCallbackProtocol | None,
enable_http_endpoint: bool,
enable_mcp_tool_trigger: bool,
) -> None:
"""Set up the HTTP trigger, entity, and MCP tool trigger for a specific agent.
Args:
agent: The agent instance
agent_name: The name to use for routing and entity registration
callback: Optional callback to receive response updates
enable_http_endpoint: Whether to create HTTP endpoint
enable_mcp_tool_trigger: Whether to create MCP tool trigger
"""
logger.debug(f"[AgentFunctionApp] Setting up functions for agent '{agent_name}'...")
if enable_http_endpoint:
self._setup_http_run_route(agent_name)
else:
logger.debug(
"[AgentFunctionApp] HTTP run route disabled for agent '%s'",
agent_name,
)
self._setup_agent_entity(agent, agent_name, callback)
if enable_mcp_tool_trigger:
agent_description = agent.description
self._setup_mcp_tool_trigger(agent_name, agent_description)
else:
logger.debug(f"[AgentFunctionApp] MCP tool trigger disabled for agent '{agent_name}'")
def _setup_http_run_route(self, agent_name: str) -> None:
"""Register the POST route that triggers agent execution.
Args:
agent_name: The agent name (used for both routing and entity identification)
"""
run_function_name = self._build_function_name(agent_name, "http")
function_name_decorator = self.function_name(run_function_name)
route_decorator = self.route(route=f"agents/{agent_name}/run", methods=["POST"])
durable_client_decorator = self.durable_client_input(client_name="client")
@function_name_decorator
@route_decorator
@durable_client_decorator
async def http_start(req: func.HttpRequest, client: df.DurableOrchestrationClient) -> func.HttpResponse:
"""HTTP trigger that calls a durable entity to execute the agent and returns the result.
Expected request body (RunRequest format):
{
"message": "user message to agent",
"thread_id": "optional conversation identifier",
"role": "user|system" (optional, default: "user"),
"response_format": {...} (optional JSON schema for structured responses),
"enable_tool_calls": true|false (optional, default: true)
}
"""
request_response_format: str = REQUEST_RESPONSE_FORMAT_JSON
thread_id: str | None = None
try:
req_body, message, request_response_format = self._parse_incoming_request(req)
thread_id = self._resolve_thread_id(req=req, req_body=req_body)
wait_for_response = self._should_wait_for_response(req=req, req_body=req_body)
logger.debug(
f"[HTTP Trigger] Message: {message}, Thread ID: {thread_id}, wait_for_response: {wait_for_response}"
)
if not message:
logger.warning("[HTTP Trigger] Request rejected: Missing message")
return self._create_http_response(
payload={"error": "Message is required"},
status_code=400,
request_response_format=request_response_format,
thread_id=thread_id,
)
session_id = self._create_session_id(agent_name, thread_id)
correlation_id = self._generate_unique_id()
logger.debug(
f"[HTTP Trigger] Calling entity to run agent using session ID: {session_id} "
f"and correlation ID: {correlation_id}"
)
entity_instance_id = df.EntityId(
name=session_id.entity_name,
key=session_id.key,
)
run_request = self._build_request_data(
req_body,
message,
correlation_id,
request_response_format,
)
logger.debug("Signalling entity %s with request: %s", entity_instance_id, run_request)
await client.signal_entity(entity_instance_id, "run", run_request)
logger.debug(f"[HTTP Trigger] Signal sent to entity {session_id}")
if wait_for_response:
result = await self._get_response_from_entity(
client=client,
entity_instance_id=entity_instance_id,
correlation_id=correlation_id,
message=message,
thread_id=thread_id,
)
logger.debug(f"[HTTP Trigger] Result status: {result.get('status', 'unknown')}")
return self._create_http_response(
payload=result,
status_code=200 if result.get("status") == "success" else 500,
request_response_format=request_response_format,
thread_id=thread_id,
)
logger.debug("[HTTP Trigger] wait_for_response disabled; returning correlation ID")
accepted_response = self._build_accepted_response(
message=message, thread_id=thread_id, correlation_id=correlation_id
)
return self._create_http_response(
payload=accepted_response,
status_code=202,
request_response_format=request_response_format,
thread_id=thread_id,
)
except IncomingRequestError as exc:
logger.warning(f"[HTTP Trigger] Request rejected: {exc!s}")
return self._create_http_response(
payload={"error": str(exc)},
status_code=exc.status_code,
request_response_format=request_response_format,
thread_id=thread_id,
)
except ValueError as exc:
logger.error(f"[HTTP Trigger] Invalid JSON: {exc!s}")
return self._create_http_response(
payload={"error": "Invalid JSON"},
status_code=400,
request_response_format=request_response_format,
thread_id=thread_id,
)
except Exception as exc:
logger.error(f"[HTTP Trigger] Error: {exc!s}", exc_info=True)
return self._create_http_response(
payload={"error": str(exc)},
status_code=500,
request_response_format=request_response_format,
thread_id=thread_id,
)
_ = http_start
def _setup_agent_entity(
self,
agent: SupportsAgentRun,
agent_name: str,
callback: AgentResponseCallbackProtocol | None,
) -> None:
"""Register the durable entity responsible for agent state.
Args:
agent: The agent instance
agent_name: The agent name (used for both entity identification and function naming)
callback: Optional callback for response updates
"""
# Use the prefixed entity name for both registration and function naming
entity_name_with_prefix = AgentSessionId.to_entity_name(agent_name)
def entity_function(context: df.DurableEntityContext) -> None:
"""Durable entity that manages agent execution and conversation state.
Operations:
- run: Execute the agent with a message
- run_agent: (Deprecated) Execute the agent with a message
- reset: Clear conversation history
"""
entity_handler = create_agent_entity(agent, callback)
entity_handler(context)
# Set function name for Azure Functions (used in function.json generation)
# Use the prefixed entity name as the function name too.
entity_function.__name__ = entity_name_with_prefix
self.entity_trigger(context_name="context", entity_name=entity_name_with_prefix)(entity_function)
def _setup_mcp_tool_trigger(self, agent_name: str, agent_description: str | None) -> None:
"""Register an MCP tool trigger for an agent using Azure Functions native MCP support.
This creates a native Azure Functions MCP tool trigger that exposes the agent
as an MCP tool, allowing it to be invoked by MCP-compatible clients.
Args:
agent_name: The agent name (used as the MCP tool name)
agent_description: Optional description for the MCP tool (shown to clients)
"""
mcp_function_name = self._build_function_name(agent_name, "mcptool")
# Define tool properties as JSON (MCP tool parameters)
tool_properties = json.dumps([
{
"propertyName": "query",
"propertyType": "string",
"description": "The query to send to the agent.",
"isRequired": True,
"isArray": False,
},
{
"propertyName": "threadId",
"propertyType": "string",
"description": "Optional thread identifier for conversation continuity.",
"isRequired": False,
"isArray": False,
},
])
function_name_decorator = self.function_name(mcp_function_name)
mcp_tool_decorator = self.mcp_tool_trigger(
arg_name="context",
tool_name=agent_name,
description=agent_description or f"Interact with {agent_name} agent",
tool_properties=tool_properties,
data_type=func.DataType.UNDEFINED,
)
durable_client_decorator = self.durable_client_input(client_name="client")
@function_name_decorator
@mcp_tool_decorator
@durable_client_decorator
async def mcp_tool_handler(context: str, client: df.DurableOrchestrationClient) -> str:
"""Handle MCP tool invocation for the agent.
Args:
context: MCP tool invocation context containing arguments (query, threadId)
client: Durable orchestration client for entity communication
Returns:
Agent response text
"""
logger.debug("[MCP Tool Trigger] Received invocation for agent: %s", agent_name)
return await self._handle_mcp_tool_invocation(agent_name=agent_name, context=context, client=client)
_ = mcp_tool_handler
logger.debug("[AgentFunctionApp] Registered MCP tool trigger for agent: %s", agent_name)
async def _handle_mcp_tool_invocation(
self, agent_name: str, context: str, client: df.DurableOrchestrationClient
) -> str:
"""Handle an MCP tool invocation.
This method processes MCP tool requests and delegates to the agent entity.
Args:
agent_name: Name of the agent being invoked
context: MCP tool invocation context as a JSON string
client: Durable orchestration client
Returns:
Agent response text
Raises:
ValueError: If required arguments are missing or context is invalid JSON
RuntimeError: If agent execution fails
"""
logger.debug("[MCP Tool Handler] Processing invocation for agent '%s'", agent_name)
# Parse JSON context string
try:
parsed_context: Any = json.loads(context)
except json.JSONDecodeError as e:
raise ValueError(f"Invalid MCP context format: {e}") from e
parsed_context = cast(Mapping[str, Any], parsed_context) if isinstance(parsed_context, dict) else {}
# Extract arguments from MCP context
arguments: dict[str, Any] = parsed_context.get("arguments", {})
# Validate required 'query' argument
query: Any = arguments.get("query")
if not query or not isinstance(query, str):
raise ValueError("MCP Tool invocation is missing required 'query' argument of type string.")
# Extract optional threadId
thread_id = arguments.get("threadId")
# Create or parse session ID
if thread_id and isinstance(thread_id, str) and thread_id.strip():
try:
session_id = AgentSessionId.parse(thread_id, agent_name=agent_name)
except ValueError as e:
logger.warning(
"Failed to parse AgentSessionId from thread_id '%s': %s. Falling back to new session ID.",
thread_id,
e,
)
session_id = AgentSessionId(name=agent_name, key=thread_id)
else:
# Generate new session ID
session_id = AgentSessionId.with_random_key(agent_name)
# Build entity instance ID
entity_instance_id = df.EntityId(
name=session_id.entity_name,
key=session_id.key,
)
# Create run request
correlation_id = self._generate_unique_id()
run_request = self._build_request_data(
req_body={"message": query, "role": "user"},
message=query,
correlation_id=correlation_id,
request_response_format=REQUEST_RESPONSE_FORMAT_TEXT,
)
query_preview = query[:50] + "..." if len(query) > 50 else query