Coverage for apps / backend / src / orcheo_backend / app / mcp_server / catalog_tools.py: 99%

58 statements  

« prev     ^ index     » next       coverage.py v7.13.0, created at 2026-11-16 14:04 +0000

1"""MCP tools describing the nodes, edges and agent tools this server provides.""" 

2 

3from __future__ import annotations 

4from collections.abc import Callable 

5from importlib import import_module 

6from typing import Annotated, Any, Literal, Protocol 

7from fastmcp import FastMCP 

8from fastmcp.exceptions import ToolError 

9from pydantic import Field 

10from orcheo.plugins import ensure_plugins_loaded 

11from orcheo_backend.app.mcp_server._shared import READ_ONLY 

12 

13 

14CatalogKind = Literal["node", "edge", "agent_tool"] 

15KindArg = Annotated[ 

16 CatalogKind, 

17 Field( 

18 description=( 

19 "'node' for workflow nodes, 'edge' for conditional edges, " 

20 "'agent_tool' for tools that agent nodes can call." 

21 ) 

22 ), 

23] 

24 

25 

26class _Metadata(Protocol): 

27 name: str 

28 category: str 

29 description: str 

30 

31 

32class _Registry(Protocol): 

33 def list_metadata(self) -> list[Any]: ... # pragma: no cover 

34 

35 def get_metadata(self, name: str) -> Any: ... # pragma: no cover 

36 

37 

38def _registry(kind: CatalogKind) -> tuple[_Registry, Callable[[str], Any]]: 

39 """Return the registry for ``kind`` and its component lookup function.""" 

40 ensure_plugins_loaded() 1eabc

41 if kind == "node": 1eabc

42 from orcheo.nodes.registry import registry 1c

43 

44 return registry, registry.get_node 1c

45 if kind == "edge": 1eab

46 from orcheo.edges.registry import edge_registry 1b

47 

48 return edge_registry, edge_registry.get_edge 1b

49 # Importing the tools module registers the built-in agent tools. 

50 import_module("orcheo.nodes.ai.tools.tools") 1ea

51 from orcheo.nodes.ai.tools.registry import tool_registry 1ea

52 

53 return tool_registry, tool_registry.get_tool 1ea

54 

55 

56def _summary(metadata: _Metadata) -> dict[str, str]: 

57 return { 1abc

58 "name": metadata.name, 

59 "category": metadata.category, 

60 "description": metadata.description, 

61 } 

62 

63 

64def _component_schema(component: Any) -> dict[str, Any] | None: 

65 """Return the JSON schema describing a component's configuration.""" 

66 args_schema = getattr(component, "args_schema", None) 1fabc

67 if args_schema is not None and hasattr(args_schema, "model_json_schema"): 1fabc

68 return args_schema.model_json_schema() 1a

69 if hasattr(component, "model_json_schema"): 1fbc

70 return component.model_json_schema() 1bc

71 return None 1f

72 

73 

74def register_catalog_tools(server: FastMCP) -> None: 

75 """Register node, edge and agent-tool discovery tools on ``server``.""" 

76 

77 @server.tool(annotations=READ_ONLY) 2g h i j k l m n o p q e a b c r s t u v w x y z A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - . / : ; = ? @ [ ] ^ _ ` { | } ~ abbbcbdbebfbgbhbibjbkblbmbnbobpbqbrbsbtbubvbwbxbybzbAbBbCbDbEbFbGbHbIbJbKbLbMbNbObPbQbRbSbTbUbVb

78 def list_components( 2g h i j k l m n o p q e a b c r s t u v w x y z A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - . / : ; = ? @ [ ] ^ _ ` { | } ~ abbbcbdbebfbgbhbibjbkblbmbnbobpbqbrbsbtbubvbwbxbybzbAbBbCbDbEbFbGbHbIbJbKbLbMbNbObPbQbRbSbTbUbVb

79 kind: KindArg, 

80 query: Annotated[ 

81 str | None, 

82 Field(description="Case-insensitive filter on name or category."), 

83 ] = None, 

84 ) -> dict[str, Any]: 

85 """List the registered nodes, edges or agent tools, including plugins.""" 

86 registry, _ = _registry(kind) 1abc

87 entries = [_summary(item) for item in registry.list_metadata()] 1abc

88 if query: 1abc

89 needle = query.lower() 1abc

90 entries = [ 1abc

91 entry 

92 for entry in entries 

93 if needle in entry["name"].lower() 

94 or needle in entry["category"].lower() 

95 ] 

96 return {"kind": kind, "components": entries} 1abc

97 

98 @server.tool(annotations=READ_ONLY) 2g h i j k l m n o p q e a b c r s t u v w x y z A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - . / : ; = ? @ [ ] ^ _ ` { | } ~ abbbcbdbebfbgbhbibjbkblbmbnbobpbqbrbsbtbubvbwbxbybzbAbBbCbDbEbFbGbHbIbJbKbLbMbNbObPbQbRbSbTbUbVb

99 def describe_component( 2g h i j k l m n o p q e a b c r s t u v w x y z A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - . / : ; = ? @ [ ] ^ _ ` { | } ~ abbbcbdbebfbgbhbibjbkblbmbnbobpbqbrbsbtbubvbwbxbybzbAbBbCbDbEbFbGbHbIbJbKbLbMbNbObPbQbRbSbTbUbVb

100 kind: KindArg, 

101 name: Annotated[str, Field(description="Registered component name.")], 

102 ) -> dict[str, Any]: 

103 """Return a node, edge or agent tool's description and config schema.""" 

104 registry, lookup = _registry(kind) 1eabc

105 metadata = registry.get_metadata(name) 1eabc

106 component = lookup(name) 1eabc

107 if metadata is None or component is None: 1eabc

108 raise ToolError( 1e

109 f"No {kind.replace('_', ' ')} named '{name}' is registered." 

110 ) 

111 result: dict[str, Any] = _summary(metadata) 1abc

112 schema = _component_schema(component) 1abc

113 if schema is not None: 113 ↛ 115line 113 didn't jump to line 115 because the condition on line 113 was always true1abc

114 result["schema"] = schema 1abc

115 return result 1abc

116 

117 

118__all__ = ["register_catalog_tools"]