Skip to content

Commit 0bafec6

Browse files
sanitize tool name ensuring safety
1 parent 55d2f04 commit 0bafec6

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

src/pieces/mcp/gateway.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,14 +180,23 @@ def _get_error_message_for_tool(self, tool_name: str) -> str:
180180

181181
if not is_valid:
182182
return error_message
183-
183+
tool_name = self._sanitize_tool_name(tool_name)
184184
# If all validations pass but we still have an error, return generic message
185+
185186
return (
186187
f"Unable to execute '{tool_name}' tool. Please ensure PiecesOS is running "
187188
"and try again. If the problem persists, run:\n\n"
188189
"`pieces restart`"
189190
)
190191

192+
def _sanitize_tool_name(self, tool_name: str) -> str:
193+
"""Sanitize tool name for safe inclusion in messages."""
194+
import re
195+
196+
# Remove control characters and limit length
197+
sanitized = re.sub(r"[^\w\s\-_.]", "", tool_name)
198+
return sanitized[:100] # Limit to reasonable length
199+
191200
def _get_tools_hash(self, tools):
192201
"""Generate a hash of the tools list for change detection."""
193202
if not tools:

0 commit comments

Comments
 (0)