|
4 | 4 | Provides the EnrichMCP class for creating MCP applications. |
5 | 5 | """ |
6 | 6 |
|
| 7 | +import inspect |
7 | 8 | import warnings |
8 | 9 | from collections.abc import Callable |
9 | 10 | from typing import ( |
|
24 | 25 | from .cache import CacheBackend, ContextCache, MemoryCache |
25 | 26 | from .context import EnrichContext |
26 | 27 | from .entity import EnrichModel |
| 28 | +from .parameter import EnrichParameter |
27 | 29 | from .relationship import Relationship |
28 | 30 |
|
29 | 31 | # Type variables |
@@ -320,6 +322,52 @@ def describe_model(self) -> str: |
320 | 322 |
|
321 | 323 | return "\n".join(lines) |
322 | 324 |
|
| 325 | + def _append_enrichparameter_hints(self, description: str, fn: Callable[..., Any]) -> str: |
| 326 | + """Append ``EnrichParameter`` metadata to a description string.""" |
| 327 | + |
| 328 | + hints: list[str] = [] |
| 329 | + try: |
| 330 | + sig = inspect.signature(fn) |
| 331 | + except (TypeError, ValueError): # pragma: no cover - defensive |
| 332 | + return description |
| 333 | + |
| 334 | + for param in sig.parameters.values(): |
| 335 | + default = param.default |
| 336 | + annotation = param.annotation |
| 337 | + |
| 338 | + if isinstance(default, EnrichParameter): |
| 339 | + if annotation is EnrichContext: |
| 340 | + # Context parameters are stripped from the final tool |
| 341 | + # interface so hints would be confusing to the agent. |
| 342 | + continue |
| 343 | + |
| 344 | + param_type = "Any" |
| 345 | + if annotation is not inspect.Parameter.empty: |
| 346 | + if get_origin(annotation) is Literal: |
| 347 | + values = ", ".join(repr(v) for v in get_args(annotation)) |
| 348 | + param_type = f"Literal[{values}]" |
| 349 | + else: |
| 350 | + param_type = getattr(annotation, "__name__", str(annotation)) |
| 351 | + |
| 352 | + parts = [param_type] |
| 353 | + if default.description: |
| 354 | + parts.append(default.description) |
| 355 | + if default.examples: |
| 356 | + joined = ", ".join(map(str, default.examples)) |
| 357 | + parts.append(f"examples: {joined}") |
| 358 | + if default.metadata: |
| 359 | + meta = ", ".join(f"{k}: {v}" for k, v in default.metadata.items()) |
| 360 | + parts.append(meta) |
| 361 | + |
| 362 | + hints.append(f"{param.name} - {'; '.join(parts)}") |
| 363 | + |
| 364 | + if hints: |
| 365 | + description = ( |
| 366 | + description.rstrip() + "\n\nParameter hints:\n" + "\n".join(f"- {h}" for h in hints) |
| 367 | + ) |
| 368 | + |
| 369 | + return description |
| 370 | + |
323 | 371 | def retrieve( |
324 | 372 | self, |
325 | 373 | func: Callable[..., Any] | None = None, |
@@ -369,6 +417,9 @@ def decorator(fn: Callable[..., Any]) -> Callable[..., Any]: |
369 | 417 | if resource_desc == fn.__doc__ and resource_desc: |
370 | 418 | resource_desc = resource_desc.strip() |
371 | 419 |
|
| 420 | + # Append EnrichParameter parameter hints |
| 421 | + resource_desc = self._append_enrichparameter_hints(resource_desc, fn) |
| 422 | + |
372 | 423 | # Store the resource for testing |
373 | 424 | self.resources[resource_name] = fn |
374 | 425 | # Create and apply the MCP tool decorator |
|
0 commit comments