Skip to content

Commit 9313108

Browse files
committed
mcp(fix[option_tools]): Validate scope parameter
why: Invalid scope silently fell through to server scope, making it impossible for users to detect typos like "global" vs "server". what: - Check _SCOPE_MAP.get() result, raise ToolError if scope is invalid - Add test for invalid scope
1 parent 9a22f10 commit 9313108

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

src/libtmux/mcp/tools/option_tools.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@ def _resolve_option_target(
3636
server = _get_server(socket_name=socket_name)
3737
opt_scope = _SCOPE_MAP.get(scope) if scope is not None else None
3838

39+
if scope is not None and opt_scope is None:
40+
from fastmcp.exceptions import ToolError
41+
42+
valid = ", ".join(sorted(_SCOPE_MAP))
43+
msg = f"Invalid scope: {scope!r}. Valid: {valid}"
44+
raise ToolError(msg)
45+
3946
if target is not None and opt_scope is not None:
4047
if opt_scope == OptionScope.Session:
4148
return _resolve_session(server, session_name=target), opt_scope

tests/mcp/test_option_tools.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
import json
66
import typing as t
77

8+
import pytest
9+
from fastmcp.exceptions import ToolError
10+
811
from libtmux.mcp.tools.option_tools import set_option, show_option
912

1013
if t.TYPE_CHECKING:
@@ -25,6 +28,16 @@ def test_show_option(mcp_server: Server, mcp_session: Session) -> None:
2528
assert "value" in data
2629

2730

31+
def test_show_option_invalid_scope(mcp_server: Server, mcp_session: Session) -> None:
32+
"""show_option raises ToolError on invalid scope."""
33+
with pytest.raises(ToolError, match="Invalid scope"):
34+
show_option(
35+
option="base-index",
36+
scope="global",
37+
socket_name=mcp_server.socket_name,
38+
)
39+
40+
2841
def test_set_option(mcp_server: Server, mcp_session: Session) -> None:
2942
"""set_option sets a tmux option."""
3043
result = set_option(

0 commit comments

Comments
 (0)