cwl2mcp 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
cwl2mcp/__about__.py ADDED
@@ -0,0 +1,15 @@
1
+ # Copyright 2026 Terradue
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ __version__ = "0.1.0"
cwl2mcp/__init__.py ADDED
@@ -0,0 +1,19 @@
1
+ # Copyright 2026 Terradue
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ """Derive validated MCP tool definitions from parsed CWL processes."""
16
+
17
+ from cwl2mcp.__about__ import __version__
18
+
19
+ __all__ = ["__version__"]
cwl2mcp/converter.py ADDED
@@ -0,0 +1,142 @@
1
+ # Copyright 2026 Terradue
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ """Convert parsed CWL processes and their MCP hints into tool definitions."""
16
+
17
+ import re
18
+ from collections.abc import Mapping
19
+ from typing import Any
20
+
21
+ from cwl2ogc import BaseCWLtypes2OGCConverter
22
+ from cwl_utils.parser import Process
23
+ from schema_salad.runtime import LoadingOptions
24
+
25
+ from .mcp_hint import Tool as ToolHint
26
+ from .mcp_tool import McpTool, ToolAnnotations
27
+
28
+
29
+ def to_kebab_case(name: str) -> str:
30
+ """Convert a camel-case metadata property name to snake case."""
31
+ # Insert underscore between lowercase/digit and uppercase,
32
+ # and between consecutive uppercase and uppercase+lowercase
33
+ separated_words = re.sub("(.)([A-Z][a-z]+)", r"\1-\2", name)
34
+ return re.sub("([a-z0-9])([A-Z])", r"\1-\2", separated_words).lower()
35
+
36
+
37
+ def _expand_hint_class(hint_class: str, namespaces: Mapping[str, str]) -> str:
38
+ prefix, separator, local_name = hint_class.partition(":")
39
+ if separator and prefix in namespaces:
40
+ return namespaces[prefix] + local_name
41
+ return hint_class
42
+
43
+
44
+ def _load_tool_hint(hint: Mapping[str, object], process: Process) -> ToolHint:
45
+ """Validate a hint while preserving its literal MCP callable name.
46
+
47
+ Salad requires the shared ``name`` predicate to use identifier semantics.
48
+ MCP names are literal strings, so retain the validated input spelling
49
+ instead of the URI that Salad expands relative to the CWL process.
50
+ """
51
+ tool_hint = ToolHint.fromDoc(
52
+ hint,
53
+ process.id,
54
+ LoadingOptions(
55
+ namespaces=dict(process.loadingOptions.namespaces),
56
+ fileuri=process.loadingOptions.fileuri,
57
+ # A callable name does not refer to another CWL document object.
58
+ no_link_check=True,
59
+ ),
60
+ )
61
+ name = hint.get("name")
62
+ if isinstance(name, str):
63
+ tool_hint.name = name
64
+ return tool_hint
65
+
66
+
67
+ def get_tool_hint(process: Process) -> ToolHint | None:
68
+ """Load the first MCP Tool hint from a parsed CWL process.
69
+
70
+ Already parsed Tool hints are returned unchanged. Extension mappings are
71
+ matched by their full class URI or a prefix declared by the process.
72
+
73
+ Returns:
74
+ The matching hint, or None when the process has no MCP Tool hint.
75
+
76
+ Raises:
77
+ schema_salad.exceptions.ValidationException: If a matching mapping
78
+ does not conform to the hint schema.
79
+ """
80
+ namespaces = process.loadingOptions.namespaces
81
+ for hint in process.hints or []:
82
+ if isinstance(hint, ToolHint):
83
+ return hint
84
+ if not isinstance(hint, Mapping):
85
+ continue
86
+ hint_class = hint.get("class")
87
+ if not isinstance(hint_class, str):
88
+ continue
89
+ if (
90
+ _expand_hint_class(hint_class, namespaces)
91
+ == "https://eoap.github.io/cwl-mcp-extension#Tool"
92
+ ):
93
+ return _load_tool_hint(hint, process)
94
+ return None
95
+
96
+
97
+ def to_mcp_tool(process: Process, meta_prefix: str) -> McpTool:
98
+ """Convert a process, using its optional MCP hint for tool metadata.
99
+
100
+ Args:
101
+ process: Parsed CWL process to expose as a tool.
102
+ meta_prefix: Prefix for custom MCP metadata keys.
103
+
104
+ Raises:
105
+ schema_salad.exceptions.ValidationException: If the MCP hint is invalid.
106
+ """
107
+ tool_hint = get_tool_hint(process)
108
+ converter = BaseCWLtypes2OGCConverter(process)
109
+
110
+ return McpTool(
111
+ name=tool_hint.name if tool_hint and tool_hint.name else process.id,
112
+ title=tool_hint.title if tool_hint and tool_hint.title else process.label or None,
113
+ description=process.doc if process.doc else None,
114
+ input_schema=dict(converter.get_inputs_json_schema()),
115
+ output_schema=dict(converter.get_outputs_json_schema()),
116
+ annotations=ToolAnnotations(
117
+ read_only_hint=tool_hint.annotations.readOnlyHint,
118
+ destructive_hint=tool_hint.annotations.destructiveHint,
119
+ idempotent_hint=tool_hint.annotations.idempotentHint,
120
+ open_world_hint=tool_hint.annotations.openWorldHint,
121
+ )
122
+ if tool_hint and tool_hint.annotations
123
+ else None,
124
+ field_meta={
125
+ f"{meta_prefix}-{to_kebab_case(property_name)}": property_value
126
+ for property_name in ("aliases", "useWhen", "doNotUseWhen")
127
+ if (property_value := getattr(tool_hint, property_name, None))
128
+ },
129
+ )
130
+
131
+
132
+ def to_mcp_tool_dict(process: Process, meta_prefix: str) -> dict[str, Any]:
133
+ """Convert a process, using its optional MCP hint for tool metadata.
134
+
135
+ Args:
136
+ process: Parsed CWL process to expose as a tool.
137
+ meta_prefix: Prefix for custom MCP metadata keys.
138
+
139
+ Raises:
140
+ schema_salad.exceptions.ValidationException: If the MCP hint is invalid.
141
+ """
142
+ return to_mcp_tool(process, meta_prefix).model_dump(by_alias=True, exclude_none=True)