-
Notifications
You must be signed in to change notification settings - Fork 98
Expand file tree
/
Copy path__init__.py
More file actions
867 lines (729 loc) Β· 33.3 KB
/
Copy path__init__.py
File metadata and controls
867 lines (729 loc) Β· 33.3 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
"""Concierge SDK - Structured AI workflows with staged tool execution"""
from __future__ import annotations
import subprocess
import time
from enum import Enum
from functools import wraps
from pathlib import Path
from typing import Any, Callable, List, Dict, Optional
import mcp.types as types
from mcp.types import Tool as MCPTool
from mcp.server.fastmcp import FastMCP
from mcp.server.fastmcp.server import Context
from concierge.backends.vanilla_backend import VanillaBackend
from concierge.backends.plan_backend import PlanBackend
from concierge.core.widget import Widget, WidgetMode
from concierge.core.telemetry import metrics, ENABLED as METRICS_ENABLED
from concierge.core.logging import ConciergeLogger, Heartbeat, RequestContext
from concierge.adapters.raw_server_adapter import RawServerAdapter
from concierge.state import get_default_backend
from concierge.state.base import StateBackend
from concierge.backends.code_backend import CodeBackend
from concierge.security.moderation import ContentModerator, ModerationConfig
from mcp.server.lowlevel.server import request_ctx
def _is_raw_server(obj: Any) -> bool:
"""Check if obj is a raw mcp.server.Server (not FastMCP)."""
try:
from mcp.server import Server as RawServer
return isinstance(obj, RawServer) and not isinstance(obj, FastMCP)
except ImportError:
return False
class ProviderType(Enum):
PLAIN = "plain"
SEARCH = "search"
PLAN = "plan"
CODE = "code"
def _get_provider_class(provider_type: ProviderType):
"""Lazy load provider to avoid importing optional dependencies."""
if provider_type == ProviderType.SEARCH:
try:
from concierge.backends.search_backend import SearchBackend
except ImportError as e:
raise ImportError(
"ProviderType.SEARCH requires the 'all' extras "
"(sentence-transformers, numpy). Install with: "
"pip install 'concierge-sdk[all]'"
) from e
return SearchBackend
if provider_type == ProviderType.PLAN:
return PlanBackend
if provider_type == ProviderType.CODE:
return CodeBackend
return VanillaBackend
class Config:
def __init__(self, max_results=5, provider_type=ProviderType.PLAIN, model=None):
self.max_results = max_results
self.provider_type = provider_type
self.model = model
IFRAME_TEMPLATE = """<!DOCTYPE html>
<html>
<head><style>*{margin:0;padding:0}iframe{width:100%;height:100vh;border:none}</style></head>
<body><iframe src="{url}"></iframe></body>
</html>"""
DEFAULT_WORKFLOW_INSTRUCTIONS = """
You are interacting with workflow which is self discoverable. This server unlocks new tools as you progress through the workflow.
You must ensure to call the relevant tools wherever applicable. Do not terminate early, the workflow will indicate when no more stages or tools are available. Do not assume you are done, unless the tools/workflow indicates this.
You are an autonomous agent performing long running tasks on the workflow. Only interrupt to ask the user if a tool requires SPECIFIC input that you dont have or need more clarity about. DO NOT ASSUME ANY DETAIL, pause and ask use when unsure.
Trust the workflow, the workflow is self-describing. Each stage transition reveals new capabilities. Your goal is to reach the terminal stage by executing tools and navigating stages.
""".strip()
class Concierge:
def __init__(
self,
server,
*,
config=Config(),
assets_dir: Optional[str] = None,
state_backend: Optional[StateBackend] = None,
workflow_instructions: Optional[str] = None,
upstream_servers: Optional[List[str]] = None,
content_moderation: Optional[ModerationConfig] = None,
tool_patches: Optional[List[Dict[str, Any]]] = None,
**fastmcp_kwargs,
):
if isinstance(server, FastMCP):
if fastmcp_kwargs:
raise ValueError(
f"Cannot pass FastMCP kwargs {list(fastmcp_kwargs)} when "
"server is already a FastMCP instance. "
"Configure these on the FastMCP instance directly."
)
self._server = server
self._is_raw_server = False
elif _is_raw_server(server):
self._server = RawServerAdapter(server)
self._is_raw_server = True
else:
self._server = FastMCP(server, **fastmcp_kwargs)
self._is_raw_server = False
self._config = config
# Set workflow instructions on the underlying server
self._workflow_instructions = workflow_instructions
existing = self._server.instructions or ""
new_instructions = workflow_instructions or DEFAULT_WORKFLOW_INSTRUCTIONS
if existing:
self._server._mcp_server.instructions = f"{existing}\n\n{new_instructions}"
else:
self._server._mcp_server.instructions = new_instructions
self._widgets: List[Widget] = []
self._pending_resources: List[types.Resource] = []
self._assets_dir = Path(assets_dir) if assets_dir else Path.cwd() / "assets"
provider_cls = _get_provider_class(config.provider_type)
self._provider = provider_cls()
self._provider.initialize(config)
# State backend (in-memory by default, or from CONCIERGE_STATE_URL env var)
self._state = state_backend or get_default_backend()
self._upstream_servers = upstream_servers or []
self._tool_patches = tool_patches or []
# Content moderation for upstream tool I/O
if content_moderation and content_moderation.enabled:
self._moderator = ContentModerator(content_moderation)
else:
self._moderator = None
self._stages: Dict[str, List[str]] = {} # stage_name -> [tool_names]
self._transitions: Dict[str, List[str]] = {} # stage_name -> [next_stages]
self._default_stage: Optional[str] = None # First stage (for new sessions)
self._enforce_completion = True # Require LLM to reach terminal stage
@classmethod
def from_openapi(
cls,
openapi_spec: dict,
*,
name: str = "OpenAPI Server",
config: Optional[Config] = None,
**kwargs,
) -> "Concierge":
"""Create a Concierge server from an OpenAPI specification.
Requires the `fastmcp` package: pip install concierge-sdk[openapi]
Args:
openapi_spec: OpenAPI schema as a dictionary
name: Name for the MCP server
config: Concierge config (provider type, etc.)
**kwargs: Additional kwargs passed to Concierge()
"""
try:
from fastmcp import FastMCP as StandaloneFastMCP
except ImportError:
raise ImportError(
"fastmcp is required for OpenAPI support. "
"Install it with: pip install concierge-sdk[openapi]"
)
standalone = StandaloneFastMCP.from_openapi(openapi_spec, name=name)
# The standalone FastMCP exposes .http_app() but not
# .streamable_http_app(). We store it and override
# streamable_http_app to use the standalone's app + telemetry.
instance = cls(name, config=config or Config(), **kwargs)
instance._openapi_standalone = standalone
return instance
@property
def enforce_completion(self) -> bool:
"""If True, instruct LLM that it must continue until reaching a terminal stage."""
return self._enforce_completion
@enforce_completion.setter
def enforce_completion(self, value: bool):
self._enforce_completion = value
def _is_terminal_stage(self, stage: str) -> bool:
"""A stage is terminal if it has no outgoing transitions."""
return not self._transitions.get(stage, [])
def _get_session_stage(self, session_id: Optional[str]) -> str:
"""Get current stage for a session. Returns default stage for new/unknown sessions."""
if session_id:
stage = self._state.get_session_stage(session_id)
if stage:
return stage
return self._default_stage or list(self._stages.keys())[0]
def _set_session_stage(self, session_id: Optional[str], stage: str) -> None:
"""Set current stage for a session."""
if session_id:
self._state.set_session_stage(session_id, stage)
@property
def stages(self) -> Dict[str, List[str]]:
"""Get stages mapping: stage_name β [tool_names]"""
return self._stages
@stages.setter
def stages(self, value: Dict[str, List[str]]):
"""Declaratively assign existing tools to stages.
Example:
app.stages = {
"browse": ["search_items", "view_details"],
"checkout": ["add_to_cart", "pay"],
}
"""
self._stages = value
@property
def transitions(self) -> Dict[str, List[str]]:
"""Get transitions: stage_name : [allowed_next_stages]"""
return self._transitions
@transitions.setter
def transitions(self, value: Dict[str, List[str]]):
"""Define allowed stage transitions.
Example:
app.transitions = {
"browse": ["checkout"],
"checkout": ["payment"],
"payment": [],
}
"""
self._transitions = value
def stage(self, name: str) -> Callable:
"""Decorator to assign a tool to a stage.
Example:
@app.stage("browse")
@app.tool()
def search_items(query: str):
return {"items": [...]}
"""
def decorator(fn: Callable) -> Callable:
tool_name = fn.__name__
if name not in self._stages:
self._stages[name] = []
if tool_name not in self._stages[name]:
self._stages[name].append(tool_name)
return fn
return decorator
def tool(self, **kwargs) -> Callable:
"""Register a tool. Delegates to underlying FastMCP.
Example:
@app.tool()
def my_tool(arg: str) -> dict:
return {"result": arg}
"""
return self._server.tool(**kwargs)
def _get_current_session_id(self) -> str:
"""Get current session ID from request context."""
ctx = request_ctx.get()
return ctx.request.headers.get("mcp-session-id")
def get_state(self, key: str, default: Any = None) -> Any:
"""Get a value from session-aware state."""
session_id = self._get_current_session_id()
value = self._state.get_state(session_id, key)
return value if value is not None else default
def set_state(self, key: str, value: Any) -> None:
"""Set a value in session-aware state."""
session_id = self._get_current_session_id()
self._state.set_state(session_id, key, value)
def clear_session_state(self, session_id: str) -> None:
"""Clear all state for a session."""
self._state.clear_session(session_id)
def _setup_staged_tools(self) -> None:
"""Override list_tools to only return current stage tools + transition tool."""
if not self._stages:
return # No stages defined, use default flat list
# Set default stage to first stage (for new sessions)
if self._default_stage is None:
self._default_stage = list(self._stages.keys())[0]
instance = self
async def _handle_next_stage(target_stage: str, ctx: Context = None) -> dict:
"""Transition to the next stage in the workflow."""
req_ctx = request_ctx.get()
session_id = req_ctx.request.headers.get("mcp-session-id")
current = instance._get_session_stage(session_id)
allowed = instance._transitions.get(current, [])
if target_stage not in allowed:
return {
"error": f"Cannot transition from '{current}' to '{target_stage}'",
"allowed_transitions": allowed,
"current_stage": current,
}
instance._set_session_stage(session_id, target_stage)
await req_ctx.session.send_notification(
types.ServerNotification(types.ToolListChangedNotification()),
related_request_id=req_ctx.request_id,
)
is_terminal = instance._is_terminal_stage(target_stage)
if is_terminal:
stage_instruction = (
"TERMINAL STAGE REACHED. No further transitions available. "
"Execute remaining tools in this stage, then provide your final summary."
)
else:
stage_instruction = (
"STAGE TRANSITIONED. New tools are now available. "
"Continue executing tools and transitioning until you reach the terminal stage."
)
result = {
"status": "transitioned",
"from_stage": current,
"to_stage": target_stage,
"message": f"Successfully transitioned from '{current}' to '{target_stage}'.",
"instruction": f"{DEFAULT_WORKFLOW_INSTRUCTIONS}\n\n{stage_instruction}",
}
return result
self._server.tool(
name="proceed_to_next_stage",
description="Proceed to the next available stage in the workflow, unlocking a new set of tools.",
)(_handle_next_stage)
# Create the terminate session tool handler
async def _handle_terminate_session(ctx: Context = None) -> dict:
"""Terminate the current workflow session and reset to initial state."""
req_ctx = request_ctx.get()
session_id = req_ctx.request.headers.get("mcp-session-id")
current = instance._get_session_stage(session_id)
initial = instance._default_stage
if session_id:
instance._state.clear_session(session_id)
await req_ctx.session.send_notification(
types.ServerNotification(types.ToolListChangedNotification()),
related_request_id=req_ctx.request_id,
)
return {
"status": "terminated",
"previous_stage": current,
"message": f"Session terminated. Workflow and state reset from '{current}' to initial stage '{initial}'. You can now start a fresh workflow or switch to a different task.",
}
self._server.tool(
name="terminate_session",
description="Terminate the current workflow session and reset to the beginning. Call this when: (1) the user wants to start over, (2) the user changes their mind and wants to do something different, (3) the user explicitly asks to stop/cancel/abort, or (4) you have completed the workflow and the user indicates they are done.",
)(_handle_terminate_session)
original_list_tools = self._server.list_tools
async def filtered_list_tools():
"""Return only tools from the current stage."""
ctx = request_ctx.get()
session_id = ctx.request.headers.get("mcp-session-id")
current_stage = instance._get_session_stage(session_id)
current_stage_tool_names = instance._stages.get(current_stage, [])
next_stages = instance._transitions.get(current_stage, [])
all_tools = await original_list_tools()
visible_tools = []
for tool in all_tools:
if tool.name in current_stage_tool_names:
tool_copy = tool.model_copy()
tool_copy.description = (
f"[{current_stage}] {tool.description or ''}"
)
visible_tools.append(tool_copy)
if next_stages:
stage_list = ", ".join(f"'{s}'" for s in next_stages)
visible_tools.append(
MCPTool(
name="proceed_to_next_stage",
description=(
f"Proceed to the next available stage in the workflow. "
f"This will unlock a new set of tools and allow you to continue. "
f"Currently in stage '{current_stage}'. "
f"Available stages to proceed to: {stage_list}."
),
inputSchema={
"type": "object",
"title": "StageTransitionRequest",
"description": "Request to transition to a different stage in the workflow.",
"properties": {
"target_stage": {
"type": "string",
"title": "Target Stage",
"description": (
f"The name of the stage to transition to. "
f"Must be one of the available stages: {stage_list}."
),
"enum": next_stages,
}
},
"required": ["target_stage"],
"additionalProperties": False,
},
)
)
visible_tools.append(
MCPTool(
name="terminate_session",
description=(
"Terminate the current workflow session and reset to the beginning. "
"You should typically call this when: (1) the user wants to start over, (2) the user changes their mind and wants to do something different, "
"(3) the user explicitly asks to stop/cancel/abort, or (4) you have completed the workflow and the user indicates they are done."
),
inputSchema={
"type": "object",
"title": "TerminateSessionRequest",
"description": "Request to terminate the current workflow session.",
"properties": {},
"required": [],
"additionalProperties": False,
},
)
)
return visible_tools
self._server.list_tools = filtered_list_tools
self._server._mcp_server.list_tools()(filtered_list_tools)
def _get_widget_meta(self, w: Widget) -> dict:
return {
"openai/outputTemplate": w.uri,
"openai/widgetAccessible": w.widget_accessible,
"openai/toolInvocation/invoking": w.invoking,
"openai/toolInvocation/invoked": w.invoked,
}
def _setup_resource_handler(self) -> None:
original_list_resources = self._server.list_resources
widget_resources = self._pending_resources
async def _list_all_resources() -> List[types.Resource]:
fastmcp_resources = await original_list_resources()
return fastmcp_resources + widget_resources
self._server._mcp_server.list_resources()(_list_all_resources)
# todo, isoalte the stage transition logic vs the UI/html/widget logic in separate files.
def _get_widget_html(self, widget: Widget) -> str:
mode = widget.mode
if mode == WidgetMode.HTML:
return widget.html
if mode == WidgetMode.URL:
return IFRAME_TEMPLATE.format(url=widget.url)
if mode == WidgetMode.ENTRYPOINT:
dist_path = self._assets_dir / "dist" / widget.dist_file
if not dist_path.exists():
raise FileNotFoundError(
f"Widget {widget.name}: dist/{widget.dist_file} not found. "
f"Run 'npm run build' in {self._assets_dir}"
)
return dist_path.read_text()
if mode == WidgetMode.DYNAMIC:
if not hasattr(widget, "_last_args") or widget._last_args is None:
raise ValueError(f"Widget {widget.name}: call the tool first")
return widget.html_fn(widget._last_args)
raise ValueError(f"Unknown widget mode: {mode}")
def _setup_read_resource_handler(self) -> None:
widgets_by_uri = {w.uri: w for w in self._widgets}
provider_resource_results = self._provider_resource_results
get_html = self._get_widget_html
original_handler = self._server._mcp_server.request_handlers.get(
types.ReadResourceRequest
)
async def _read_resource_with_meta(
req: types.ReadResourceRequest,
) -> types.ServerResult:
uri_str = str(req.params.uri)
widget = widgets_by_uri.get(uri_str)
if widget:
text = get_html(widget)
contents = [
types.TextResourceContents(
uri=widget.uri,
mimeType=widget.mime_type,
text=text,
_meta=self._get_widget_meta(widget),
)
]
return types.ServerResult(types.ReadResourceResult(contents=contents))
if uri_str in provider_resource_results:
return provider_resource_results[uri_str]
if original_handler:
return await original_handler(req)
raise ValueError(f"Unknown resource: {uri_str}")
self._server._mcp_server.request_handlers[types.ReadResourceRequest] = (
_read_resource_with_meta
)
def _run_widget_builds(self):
needs_build = any(w.mode == WidgetMode.ENTRYPOINT for w in self._widgets)
if not needs_build:
return
package_json = self._assets_dir / "package.json"
if not package_json.exists():
raise FileNotFoundError(
f"{self._assets_dir}/package.json not found. "
f"Widgets using entrypoint mode require a build system."
)
print("Installing web dependencies...", flush=True)
result = subprocess.run(
["npm", "install"],
cwd=str(self._assets_dir),
capture_output=True,
text=True,
)
if result.returncode != 0:
raise RuntimeError(f"npm install failed:\n{result.stderr}")
print("Building widgets...", flush=True)
result = subprocess.run(
["npm", "run", "build"],
cwd=str(self._assets_dir),
capture_output=True,
text=True,
)
if result.returncode != 0:
raise RuntimeError(f"Build failed:\n{result.stdout}\n{result.stderr}")
print("Build complete", flush=True)
def _finalize(self):
"""Setup all handlers. Called before run() or streamable_http_app()."""
if getattr(self, "_finalized", False):
return
self._finalized = True
self._run_widget_builds()
tools = self._server._tool_manager.list_tools()
self._provider.index_tools(tools)
served_tools = self._provider.serve_tools()
self._server._tool_manager._tools = {t.name: t for t in served_tools}
self._provider_resource_results: Dict[str, Any] = {}
for resource, result in self._provider.serve_resources():
self._pending_resources.append(resource)
self._provider_resource_results[str(resource.uri)] = result
self._setup_resource_handler()
self._setup_read_resource_handler()
self._setup_staged_tools()
# Install proxy before metrics β proxy replaces handlers,
# metrics wraps them, so metrics must be outermost.
if self._upstream_servers:
from concierge.proxy import install_proxy_handlers
install_proxy_handlers(self)
self._setup_metrics()
def _setup_metrics(self):
if not METRICS_ENABLED:
return
handlers = self._server._mcp_server.request_handlers
if types.CallToolRequest in handlers:
original = handlers[types.CallToolRequest]
async def wrapped_call(req: types.CallToolRequest) -> types.ServerResult:
metrics.ensure_started()
RequestContext.set(tool=req.params.name, method="tools/call")
ctx = request_ctx.get()
session_id = (
ctx.request.headers.get("mcp-session-id", "unknown")
if ctx and ctx.request
else "unknown"
)
client_name = None
if (
ctx
and ctx.session
and ctx.session.client_params
and ctx.session.client_params.clientInfo
):
client_name = ctx.session.client_params.clientInfo.name
start = time.perf_counter()
is_error, error_msg = False, None
try:
return await original(req)
except Exception as e:
is_error, error_msg = True, str(e)
raise
finally:
RequestContext.clear()
metrics.track(
"mcp:tools/call",
session_id=session_id,
resource_name=req.params.name,
arguments=req.params.arguments,
duration_ms=int((time.perf_counter() - start) * 1000),
is_error=is_error,
error_message=error_msg,
client=client_name,
)
handlers[types.CallToolRequest] = wrapped_call
if types.ReadResourceRequest in handlers:
original_read = handlers[types.ReadResourceRequest]
async def wrapped_read(
req: types.ReadResourceRequest,
) -> types.ServerResult:
metrics.ensure_started()
RequestContext.set(method="resources/read")
ctx = request_ctx.get()
session_id = (
ctx.request.headers.get("mcp-session-id", "unknown")
if ctx and ctx.request
else "unknown"
)
client_name = None
if (
ctx
and ctx.session
and ctx.session.client_params
and ctx.session.client_params.clientInfo
):
client_name = ctx.session.client_params.clientInfo.name
start = time.perf_counter()
is_error, error_msg = False, None
try:
return await original_read(req)
except Exception as e:
is_error, error_msg = True, str(e)
raise
finally:
RequestContext.clear()
metrics.track(
"mcp:resources/read",
session_id=session_id,
resource_name=str(req.params.uri),
duration_ms=int((time.perf_counter() - start) * 1000),
is_error=is_error,
error_message=error_msg,
client=client_name,
)
handlers[types.ReadResourceRequest] = wrapped_read
def run(self, *args, **kwargs):
ConciergeLogger.configure()
self._finalize()
metrics.start()
return self._server.run(*args, **kwargs)
@staticmethod
def _wrap_with_heartbeat(app):
"""Wrap an ASGI app to start the heartbeat on startup."""
original_app = app
async def wrapper(scope, receive, send):
if scope["type"] == "lifespan":
Heartbeat.start()
return await original_app(scope, receive, send)
return wrapper
def streamable_http_app(self, **kwargs):
ConciergeLogger.configure()
# OpenAPI path: use standalone fastmcp's app with telemetry
if hasattr(self, "_openapi_standalone"):
standalone = self._openapi_standalone
app = standalone.http_app()
# Wire telemetry into the standalone server's request handlers
if METRICS_ENABLED:
handlers = standalone._mcp_server.request_handlers
if types.CallToolRequest in handlers:
original = handlers[types.CallToolRequest]
async def wrapped_call(
req: types.CallToolRequest,
) -> types.ServerResult:
metrics.ensure_started()
ctx = request_ctx.get()
session_id = (
ctx.request.headers.get("mcp-session-id", "unknown")
if ctx and ctx.request
else "unknown"
)
client_name = None
if (
ctx
and ctx.session
and ctx.session.client_params
and ctx.session.client_params.clientInfo
):
client_name = ctx.session.client_params.clientInfo.name
start = time.perf_counter()
is_error, error_msg = False, None
try:
return await original(req)
except Exception as e:
is_error, error_msg = True, str(e)
raise
finally:
metrics.track(
"mcp:tools/call",
session_id=session_id,
resource_name=req.params.name,
arguments=req.params.arguments,
duration_ms=int((time.perf_counter() - start) * 1000),
is_error=is_error,
error_message=error_msg,
client=client_name,
)
handlers[types.CallToolRequest] = wrapped_call
metrics.start()
return self._wrap_with_heartbeat(app)
self._finalize()
metrics.start()
try:
app = self._server.streamable_http_app(**kwargs)
except TypeError:
app = self._server.streamable_http_app()
return self._wrap_with_heartbeat(app)
def widget(
self,
uri: str,
# Mode 1: Inline HTML
html: Optional[str] = None,
# Mode 2: External URL
url: Optional[str] = None,
# Mode 3: Entrypoint (filename in entrypoints/)
entrypoint: Optional[str] = None,
# Mode 4: Dynamic function (takes args dict, returns HTML string)
html_fn: Optional[Callable[[dict], str]] = None,
# Metadata
name: Optional[str] = None,
title: Optional[str] = None,
description: Optional[str] = None,
invoking: str = "Loading...",
invoked: str = "Done",
annotations: Optional[dict] = None,
) -> Callable:
def decorator(fn: Callable) -> Callable:
w = Widget(
uri=uri,
html=html,
url=url,
entrypoint=entrypoint,
html_fn=html_fn,
name=name or fn.__name__,
title=title,
description=description or fn.__doc__,
invoking=invoking,
invoked=invoked,
annotations=annotations,
)
self._widgets.append(w)
self._pending_resources.append(
types.Resource(
uri=w.uri,
name=w.name,
title=w.title,
description=w.description,
mimeType=w.mime_type,
_meta=self._get_widget_meta(w),
)
)
@wraps(fn)
async def wrapped(*args, **kwargs) -> types.CallToolResult:
result = await fn(*args, **kwargs)
if w.html_fn:
w._last_args = result
return types.CallToolResult(
content=[types.TextContent(type="text", text=w.invoked)],
structuredContent=result,
_meta={
"openai/toolInvocation/invoking": w.invoking,
"openai/toolInvocation/invoked": w.invoked,
},
)
self._server.tool(
name=w.name,
title=w.title,
description=w.description,
annotations=w.annotations,
meta={
"openai/outputTemplate": w.uri,
"openai/widgetAccessible": w.widget_accessible,
"openai/toolInvocation/invoking": w.invoking,
"openai/toolInvocation/invoked": w.invoked,
},
)(wrapped)
return fn
return decorator
def __getattr__(self, name):
return getattr(self._server, name)