Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/SECURITY.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Supported Versions
| Version | Supported |
| ------- | ------------------ |
| 0.4.1 | :white_check_mark: |
| 0.4.0 | :white_check_mark: |
| 0.3.0 | :white_check_mark: |
| 0.2.19 | :white_check_mark: |
Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "golf-mcp"
version = "0.4.0"
version = "0.4.1"
description = "Framework for building MCP servers"
authors = [
{name = "Antoni Gmitruk", email = "antoni@golf.dev"}
Expand Down Expand Up @@ -72,7 +72,7 @@ golf = ["examples/**/*"]

[tool.poetry]
name = "golf-mcp"
version = "0.4.0"
version = "0.4.1"
description = "Framework for building MCP servers with zero boilerplate"
authors = ["Antoni Gmitruk <antoni@golf.dev>"]
license = "Apache-2.0"
Expand Down
2 changes: 1 addition & 1 deletion src/golf/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = "0.4.0"
__version__ = "0.4.1"

from golf.decorators import prompt, resource, tool

Expand Down
73 changes: 66 additions & 7 deletions src/golf/core/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from rich.console import Console

console = Console()
_UNSET = object()


class ComponentType(str, Enum):
Expand Down Expand Up @@ -629,13 +630,8 @@ def _process_tool(self, component: ParsedComponent, tree: ast.Module) -> None:
input_class = node
elif node.name == "Output":
output_class = node
# Look for annotations assignment
elif isinstance(node, ast.Assign):
for target in node.targets:
if isinstance(target, ast.Name) and target.id == "annotations":
if isinstance(node.value, ast.Dict):
annotations = self._extract_dict_from_ast(node.value)
break
elif extracted := self._extract_annotations_assignment(node, component.name):
annotations = extracted

# Process Input class if found
if input_class:
Expand Down Expand Up @@ -887,6 +883,69 @@ def _type_hint_to_json_type(self, type_hint: str) -> str:
else:
return "string"

def _extract_annotations_assignment(self, node: ast.AST, component_name: str) -> dict[str, Any] | None:
"""Extract a module-level `annotations` dict from an assignment node.

Accepts `annotations = {...}` and typed forms such as
`annotations: dict = {...}`, plus `dict(...)` constructors. Returns
None when this node is not an annotations assignment.
"""
value = self._annotations_value_node(node)
if value is None:
return None

extracted = self._extract_mapping_from_ast(value)
if extracted is None:
console.print(
f"[yellow]Warning: Could not parse `annotations` in tool "
f"'{component_name}'; expected a dict literal "
f"(e.g. annotations = {{'readOnlyHint': True}}).[/yellow]"
)
return None
return extracted

def _annotations_value_node(self, node: ast.AST) -> ast.expr | None:
"""Return the RHS of a module-level `annotations` assignment, if any."""
if isinstance(node, ast.Assign):
if any(isinstance(target, ast.Name) and target.id == "annotations" for target in node.targets):
return node.value
elif isinstance(node, ast.AnnAssign) and isinstance(node.target, ast.Name) and node.target.id == "annotations":
return node.value
return None

def _extract_mapping_from_ast(self, value: ast.expr) -> dict[str, Any] | None:
"""Extract a literal mapping from a dict display or `dict(...)` call."""
if isinstance(value, ast.Dict):
return self._extract_dict_from_ast(value)
if (
isinstance(value, ast.Call)
and isinstance(value.func, ast.Name)
and value.func.id == "dict"
and not value.args
):
result: dict[str, Any] = {}
for keyword in value.keywords:
if keyword.arg is None:
if isinstance(keyword.value, ast.Dict):
result.update(self._extract_dict_from_ast(keyword.value))
else:
return None
continue
extracted = self._literal_from_ast(keyword.value)
if extracted is _UNSET:
return None
result[keyword.arg] = extracted
return result
return None

def _literal_from_ast(self, value: ast.expr) -> Any:
"""Extract a simple literal from an AST expression, or `_UNSET`."""
if isinstance(value, ast.Constant):
return value.value
if isinstance(value, ast.Name) and value.id in ("True", "False", "None"):
return {"True": True, "False": False, "None": None}[value.id]
return _UNSET

def _extract_dict_from_ast(self, dict_node: ast.Dict) -> dict[str, Any]:
"""Extract a dictionary from an AST Dict node.

Expand Down
53 changes: 53 additions & 0 deletions tests/core/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,59 @@ async def delete_file(
assert component.annotations["idempotentHint"] is False
assert component.annotations["openWorldHint"] is False

def test_parses_typed_annotations_assignment(self, sample_project: Path) -> None:
"""Typed `annotations: dict = {...}` must not be silently dropped."""
tool_file = sample_project / "tools" / "typed_annotations.py"
tool_file.write_text(
'''"""Typed annotations tool."""

from typing import Any

annotations: dict[str, Any] = {
"readOnlyHint": True,
"idempotentHint": True,
}


async def run() -> str:
"""Read data."""
return "ok"


export = run
'''
)

parser = AstParser(sample_project)
components = parser.parse_file(tool_file)

assert len(components) == 1
assert components[0].annotations == {"readOnlyHint": True, "idempotentHint": True}

def test_parses_dict_constructor_annotations(self, sample_project: Path) -> None:
"""`annotations = dict(readOnlyHint=True)` should be parsed."""
tool_file = sample_project / "tools" / "dict_ctor_annotations.py"
tool_file.write_text(
'''"""Dict constructor annotations tool."""

annotations = dict(readOnlyHint=True, destructiveHint=False)


async def run() -> str:
"""Read data."""
return "ok"


export = run
'''
)

parser = AstParser(sample_project)
components = parser.parse_file(tool_file)

assert len(components) == 1
assert components[0].annotations == {"readOnlyHint": True, "destructiveHint": False}

def test_parses_tool_with_readonly_annotations(self, sample_project: Path) -> None:
"""Test parsing a tool with read-only annotations."""
tool_file = sample_project / "tools" / "read_file.py"
Expand Down
Loading