fix(ecs-mcp-server): accept ISO-8601 string start_time/end_time in calculate_time_window#3788
Open
valter-silva-au wants to merge 1 commit into
Open
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #3788 +/- ##
=======================================
Coverage 92.93% 92.93%
=======================================
Files 987 987
Lines 82364 82378 +14
Branches 13043 13046 +3
=======================================
+ Hits 76545 76559 +14
Misses 3508 3508
Partials 2311 2311 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
…lculate_time_window MCP serializes all tool arguments as JSON, so the start_time/end_time parameters exposed by ecs_troubleshooting_tool (fetch_task_failures, fetch_service_events, fetch_task_logs) always arrive as ISO-8601 strings, never datetime objects. calculate_time_window assumed datetime objects and accessed `.tzinfo` directly, so the first attribute access raised `AttributeError: 'str' object has no attribute 'tzinfo'`, making the documented parameters completely unusable. Add an `_ensure_datetime` helper that returns None for None, parses strings via `datetime.fromisoformat` (normalizing a trailing "Z" to "+00:00" for Python 3.10 compatibility) and raises a clear ValueError on malformed input, and passes datetime objects through unchanged. Call it on both start_time and end_time at the top of calculate_time_window. This single boundary fix covers all three calling sites without touching the transformers. Naive strings (no timezone) are coerced to UTC, matching the existing behavior for naive datetime objects. Backward compatible: datetime callers are unaffected (regression-tested). Fixes awslabs#2858
c967009 to
9825a10
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
ecs_troubleshooting_tooldocuments and exposesstart_time/end_timeparameters on three actions —fetch_task_failures,fetch_service_events, andfetch_task_logs. Because MCP serializes all tool arguments as JSON, these values always arrive as ISO-8601 strings, neverdatetimeobjects. The documented parameters are therefore completely unusable: any call that supplies them crashes withAttributeError: 'str' object has no attribute 'tzinfo'.Root cause
awslabs/ecs_mcp_server/utils/time_utils.py::calculate_time_windowassumed itsstart_time/end_timearguments weredatetimeobjects and accessed.tzinfodirectly:When a string is passed, the first attribute access raises. The per-action transformers in
api/ecs_troubleshooting.pyforwardparams.get("start_time")/params.get("end_time")straight through, so the string reachescalculate_time_windowunconverted.Fix
Add a small
_ensure_datetime(value)helper intime_utils.pyand call it on bothstart_timeandend_timeat the very top ofcalculate_time_window, before the existing tz-awareness logic. The helper:NoneforNone,datetimeobjects through unchanged,datetime.fromisoformat, first normalizing a trailing"Z"to"+00:00"(Python 3.10 — the project's minimum — doesn't accept theZsuffix), and re-raises a clearValueErroron malformed input.This is a single boundary fix that covers all three calling sites without modifying the transformers. Naive strings (no timezone) are coerced to UTC, matching the function's existing behavior for naive
datetimeobjects.Testing
Added 7 unit tests in
tests/unit/utils/test_time_utils.py: Z-suffix strings, explicit+00:00offset, naive-string→UTC coercion, start-only-as-string, end-only-as-string, malformed-string→ValueError, and a regression test confirmingdatetimeobjects still work.time_utilstests: 6 → 13, all passing. Lint/format/type checks (ruff, ruff-format, mypy, pyright) pass.Backward compatibility
Fully backward compatible. Existing
datetimecallers are unaffected —_ensure_datetimepassesdatetimeobjects through untouched, covered by a dedicated regression test.Fixes #2858
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of the project license.