-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTools.cs
More file actions
152 lines (141 loc) · 6.25 KB
/
Tools.cs
File metadata and controls
152 lines (141 loc) · 6.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
using System;
using System.ComponentModel;
using AbeckDev.DbTimetable.Mcp.Services;
using ModelContextProtocol.Server;
namespace AbeckDev.DbTimetable.Mcp;
[McpServerToolType]
public static class Tools
{
/// <summary>
/// MCP Server tools for accessing Deutsche Bahn Timetable API
/// </summary>
[McpServerToolType]
public class TimetableTools
{
private readonly ITimeTableService _timeTableService;
public TimetableTools(ITimeTableService timeTableService)
{
_timeTableService = timeTableService;
}
[McpServerTool]
[Description("Get all known timetable changes for the station given by evaNo . The data includes all known changes from now on until indefinitely into the future. Once changes become obsolete (because their trip departs from the station) they are removed.")]
public async Task<string> GetFullStationChanges(
[Description("Event number (EVA number) of the train event")] string eventNo)
{
try
{
var result = await _timeTableService.GetFullChangesAsync(eventNo);
return result;
}
catch (HttpRequestException ex)
{
return $"Error fetching timetable changes: {ex.Message}";
}
catch (Exception ex)
{
return $"Unexpected error: {ex.Message}";
}
}
[McpServerTool]
[Description("Get station board (departures and arrivals) for a specific station in hourly slices. Returns XML data with train schedules.")]
public async Task<string> GetStationBoard(
[Description("EVA station number (e.g., 8000105 for Frankfurt Hauptbahnhof)")] string evaNo,
[Description("Date and time in format 'yyyy-MM-dd HH:mm' (German Time). Leave empty for current time.")] string? dateTime = null)
{
try
{
DateTime? parsedDate = null;
if (!string.IsNullOrEmpty(dateTime))
{
if (DateTime.TryParse(dateTime, out var dt))
{
parsedDate = dt;
}
else
{
return "Error: Invalid date format. Please use 'yyyy-MM-dd HH:mm' format.";
}
}
var result = await _timeTableService.GetStationBoardAsync(evaNo, parsedDate);
return result;
}
catch (HttpRequestException ex)
{
return $"Error fetching station board: {ex.Message}";
}
catch (Exception ex)
{
return $"Unexpected error: {ex.Message}";
}
}
[McpServerTool]
[Description("Get all recent changes (delays, cancellations, platform changes) for a specific station. Recent changes are always a subset of the full changes. They may equal full changes but are typically much smaller. Data includes only those changes that became known within the last 2 minutes.")]
public async Task<string> GetRecentStationChanges([Description("EVA station number (e.g., 8000105 for Frankfurt Hauptbahnhof)")] string evaNo)
{
try
{
var result = await _timeTableService.GetRecentTimetableChangesAsync(evaNo);
return result;
}
catch (HttpRequestException ex)
{
return $"Error fetching station changes: {ex.Message}";
}
catch (Exception ex)
{
return $"Unexpected error: {ex.Message}";
}
}
[McpServerTool]
[Description("Get information about stations. Returns Name, EVA number and ds100 code for the provided pattern. Can be used to find the EVA station number for a given full text station.")]
public async Task<string> GetStationInformation([Description("Either a station name (prefix), eva number, ds100/rl100 code, wildcard (*); doesn't seem to work with umlauten in station name (prefix). If unsure use the Station Name e.g. \"Dresden Hbf\" ")] string pattern)
{
try
{
var result = await _timeTableService.GetStationInformation(pattern);
return result;
}
catch (HttpRequestException ex)
{
return $"Error fetching station details: {ex.Message}";
}
catch (Exception ex)
{
return $"Unexpected error: {ex.Message}";
}
}
[McpServerTool]
[Description("Find and assess train connections between two stations. Takes station names or EVA numbers as input, validates them, finds all trains operating between the stations, checks for current delays and disruptions, and provides ranked connection options with recommendations.")]
public async Task<string> FindTrainConnections(
[Description("Starting station name or EVA number (e.g., 'Frankfurt Hbf' or '8000105')")] string stationA,
[Description("Destination station name or EVA number (e.g., 'Berlin Hbf' or '8011160')")] string stationB,
[Description("Date and time in format 'yyyy-MM-dd HH:mm' (German Time). Leave empty for current time.")] string? dateTime = null)
{
try
{
DateTime? parsedDate = null;
if (!string.IsNullOrEmpty(dateTime))
{
if (DateTime.TryParse(dateTime, out var dt))
{
parsedDate = dt;
}
else
{
return "Error: Invalid date format. Please use 'yyyy-MM-dd HH:mm' format.";
}
}
var result = await _timeTableService.FindTrainConnectionsAsync(stationA, stationB, parsedDate);
return result;
}
catch (HttpRequestException ex)
{
return $"Error finding train connections: {ex.Message}";
}
catch (Exception ex)
{
return $"Unexpected error: {ex.Message}";
}
}
}
}