Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Nov 6, 2025

Train Connection Assessment Tool Implementation ✅

Successfully implemented a comprehensive MCP server tool to assess and find train connections between stations.

✅ Completed Tasks

  • Analyze existing codebase and understand current MCP tools structure
  • Design the new FindTrainConnections MCP tool interface
  • Add service method to analyze train connections between two stations
  • Implement complete logic:
    • Validate and resolve station names to EVA IDs
    • Find trains operating between stations A and B
    • Query disruptions and delays for relevant trains using GetFullChangesAsync
    • Rank connections by best option (minimal delay, time)
    • Parse XML timetable data and extract connection information
    • Handle edge cases with proper null safety
    • Extract live delay, cancellation, and platform change data from full changes
  • Create comprehensive unit tests (12 new tests added, 42 total passing)
  • Build and test the implementation (0 errors, 0 warnings)
  • Update documentation with detailed usage examples
  • Address code review feedback (improved null safety and documentation)
  • Run security checks (0 vulnerabilities found)
  • Implement live changes checking per train using GetFullChangesAsync
  • Add tests for edge cases to reach 86.87% code coverage (above 73% threshold)

📊 Changes Summary

Files Modified: 6
Total Tests: 42 (all passing)
Code Coverage: 86.87% line coverage (exceeds 73% threshold)

  • AbeckDev.DbTimetable.Mcp/Services/TimeTableService.cs (updated to use GetFullChangesAsync and extract live changes)
  • AbeckDev.DbTimetable.Mcp.Test/TimeTableServiceTests.cs (added tests for live delay data and edge cases)

🎯 Key Features

  1. Smart Station Resolution: Accepts station names or EVA IDs, validates both stations
  2. Connection Discovery: Parses timetable XML to find trains that serve both stations
  3. Live Status Integration: Uses GetFullChangesAsync to retrieve all changes and matches them to specific trains
  4. Real-time Delay Calculation: Extracts live departure times from changes data to calculate accurate delays
  5. Cancellation Detection: Checks cancellation status from live changes data
  6. Platform Change Tracking: Detects and reports platform changes from live data
  7. Message Extraction: Shows up-to-date messages and disruption information
  8. Intelligent Ranking: Orders connections by cancellation status, delay, and departure time
  9. User-friendly Output: Provides clear, actionable recommendations with visual indicators
  10. Comprehensive Error Handling: Handles cases with no changes available and no connections found

🔒 Security Summary

CodeQL analysis completed successfully with 0 alerts found. All code changes passed security validation.

Original prompt

This section details on the original issue you should resolve

<issue_title>Feature: MCP Server Tool to Assess and find Train Connections</issue_title>
<issue_description>## Feature Request: Develop a Tool for MCP Server to Assess Train Connections

Objective:
Enable the MCP server for the Timetable API to provide a tool that takes a current station (A) and a destination station (B) as input. The tool should:

  1. Validate inputs by determining the correct EVA IDs and full names of both stations.
  2. Identify which trains regularly operate between station A and station B (A and B can be any station along the train's line).
  3. If multiple options are available, query current schedule changes (disruptions, delays, reroutes, etc.).
  4. Output for user: list of viable train connections, including which one to choose and the expected current delays due to disruptions or schedule changes.

Context and Data Examples:

  • timetable_schema.json: Shows the schema for timetable data returned by the Timetable API. Includes explanations for stop elements, messages, delays, etc.
  • recent_changes.xml: Example output of the current changes task. Shows how modifications, messages, and delays are reflected as additional XML attributes/objects in timetable schemas.
  • regular_schedule_byhour.xml: Example of the regular schedule board for one station and one hour. Shows the normal (unchanged) timetable for context.

Requirements:

  • Tool interface should accept user station input in natural language, resolve station EVA IDs and full names.
  • Implements logic to find all train lines connecting A to B given current schedule.
  • Integrates disruption/delay checking from recent_changes sources and timetable messages.
  • Output selection should include best train (minimal delay, time, etc.) and a summary of delay/disruption status for each option.

Why?

This tool will improve the usability and value of the Timetable API by providing actionable train selection advice given the real-time situation, rather than just raw data.

</issue_description>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI changed the title [WIP] Add MCP server tool to assess train connections Add FindTrainConnections MCP tool for train route analysis with real-time status Nov 6, 2025
Copilot AI requested a review from abeckDev November 6, 2025 10:17
The path variable at line 323 (originally line 316) was always empty because:

Root Cause: The code was trying to read the ppth attribute from the <s> element (stop.Attribute("ppth")), but according to the Deutsche Bahn Timetable API XML schema, the ppth (path) attribute is actually located on the <dp> (departure) or <ar> (arrival) child elements, not on the parent <s> (stop) element.

The Fix:

Moved the departureElement declaration earlier in the loop
Read the ppth attribute from departureElement instead of stop
Removed the duplicate departureElement declaration later in the code
Now the code correctly retrieves the path information from <dp ppth="..."> and should be able to find train connections that go through both stations.
@codecov
Copy link

codecov bot commented Nov 6, 2025

Codecov Report

❌ Patch coverage is 77.77778% with 46 lines in your changes missing coverage. Please review.
✅ Project coverage is 76.11%. Comparing base (2308401) to head (ed1a02c).
⚠️ Report is 8 commits behind head on main.

Files with missing lines Patch % Lines
...ckDev.DbTimetable.Mcp/Services/TimeTableService.cs 76.41% 17 Missing and 29 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main      #11      +/-   ##
==========================================
+ Coverage   72.89%   76.11%   +3.21%     
==========================================
  Files           5        5              
  Lines         107      314     +207     
  Branches        5       56      +51     
==========================================
+ Hits           78      239     +161     
- Misses         29       46      +17     
- Partials        0       29      +29     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Owner

@abeckDev abeckDev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot please check the issue with the missing live changes check and implement it according to the comment on line 356.

…rain

- Changed from GetRecentTimetableChangesAsync to GetFullChangesAsync to get all changes instead of just last 2 minutes
- Implemented proper matching of changes data to each specific train by train ID
- Extract live delay, cancellation status, platform changes, and messages from the full changes data
- Added test case to verify live delay information is properly extracted and displayed

Co-authored-by: abeckDev <[email protected]>
Copy link
Contributor Author

Copilot AI commented Nov 6, 2025

@copilot please check the issue with the missing live changes check and implement it according to the comment on line 356.

Implemented in commit adc2725. The code now uses GetFullChangesAsync to retrieve all changes for the station and matches them to each specific train by train ID to extract live delay, cancellation, platform change, and message data.

Copilot AI requested a review from abeckDev November 6, 2025 10:58
@abeckDev abeckDev marked this pull request as ready for review November 6, 2025 11:11
@abeckDev
Copy link
Owner

abeckDev commented Nov 6, 2025

@copilot add some tests for the missing "no train is available" sections in the TimeTable Service (Line 157 - 175) so that the code coverage can reach the threshold of 73%

- Added test for when GetFullChangesAsync fails (no changes available case)
- Added test for when no train connections match the destination station
- Coverage increased from previous level to 86.87% line coverage (well above 73% threshold)
- All 42 tests passing

Co-authored-by: abeckDev <[email protected]>
Copy link
Contributor Author

Copilot AI commented Nov 6, 2025

@copilot add some tests for the missing "no train is available" sections in the TimeTable Service (Line 157 - 175) so that the code coverage can reach the threshold of 73%

Added in commit ed1a02c. Created two new test cases:

  1. FindTrainConnectionsAsync_WhenNoChangesAvailable_ReturnsWarning - Tests the catch block when GetFullChangesAsync fails (lines 157-161)
  2. FindTrainConnectionsAsync_WhenNoConnectionsFound_ReturnsNoConnectionsMessage - Tests when no matching trains are found (lines 168-175)

Code coverage now at 86.87% line coverage, exceeding the 73% threshold. All 42 tests passing.

@abeckDev abeckDev merged commit aef1848 into main Nov 6, 2025
4 checks passed
@abeckDev abeckDev deleted the copilot/add-mcp-server-tool branch November 6, 2025 11:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature: MCP Server Tool to Assess and find Train Connections

2 participants