nkasmanoff commited on
Commit
203f1b7
·
1 Parent(s): 5134c73

Add application file

Browse files
Files changed (1) hide show
  1. app.py +85 -16
app.py CHANGED
@@ -1,25 +1,94 @@
1
- import gradio as gr
2
 
 
 
 
 
3
 
4
- def letter_counter(word, letter):
5
- """Count the occurrences of a specific letter in a word.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
 
7
  Args:
8
- word: The word or phrase to analyze
9
- letter: The letter to count occurrences of
 
 
 
 
 
 
 
 
 
 
 
10
 
11
- Returns:
12
- The number of times the letter appears in the word
13
  """
14
- return word.lower().count(letter.lower())
 
 
 
 
 
 
 
15
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
 
17
- demo = gr.Interface(
18
- fn=letter_counter,
19
- inputs=["text", "text"],
20
- outputs="number",
21
- title="Letter Counter",
22
- description="Count how many times a letter appears in a word",
23
- )
24
 
25
- demo.launch(mcp_server=True)
 
 
 
1
+ from typing import Any
2
 
3
+ # import httpx # This import is no longer used.
4
+ from mcp.server.fastmcp import FastMCP
5
+ from nyct_gtfs import NYCTFeed
6
+ from datetime import datetime
7
 
8
+ # Initialize FastMCP server
9
+ mcp = FastMCP("mta_subway_tracker")
10
+
11
+
12
+ # Helper function to convert datetime objects to string
13
+ def convert_datetime_to_string(datetime_obj: Any) -> str | None:
14
+ if datetime_obj is None:
15
+ return None
16
+ return datetime_obj.strftime("%H:%M:%S")
17
+
18
+
19
+ # Helper function to process train data from NYCTFeed
20
+ def get_train_info_list(trains_data: Any) -> list[dict[str, Any]]:
21
+ train_info_list_data = []
22
+ for train in trains_data:
23
+ train_info = {
24
+ "name": str(train),
25
+ "line": train.route_id,
26
+ "direction": train.direction,
27
+ "stop_time_updates": [],
28
+ }
29
+ train_info["stop_time_updates"] = [
30
+ {"stop_name": x.stop_name, "arrival": convert_datetime_to_string(x.arrival)}
31
+ for x in train.stop_time_updates
32
+ ]
33
+ train_info_list_data.append(train_info)
34
+ return train_info_list_data
35
+
36
+
37
+ @mcp.tool()
38
+ async def get_next_mta_train(
39
+ target_station: str, target_direction: str, feed_id: str = "1"
40
+ ) -> str:
41
+ """Get the next train arrival information for a given station and direction.
42
 
43
  Args:
44
+ target_station: The name of the target station (e.g., "Times Sq-42 St", "14 St-Union Sq").
45
+ target_direction: The direction of the train ("N" for Northbound / Uptown, "S" for Southbound / Downtown).
46
+ feed_id: The GTFS feed ID for the subway lines (e.g., "1" for 1,2,3,4,5,6,S lines).
47
+ Common Feed IDs:
48
+ "1": 1, 2, 3, 4, 5, 6, 7, S (42 St Shuttle)
49
+ "A": A, C, E, S (Rockaway Shuttle)
50
+ "N": N, Q, R, W
51
+ "B": B, D, F, M, S (Frankin Ave)
52
+ "L": L
53
+ "G": G
54
+ "J": J, Z
55
+ "7": 7
56
+ "SIR": Staten Island Railway
57
 
58
+ This function returns a string with the next train arrival information for the given station and direction.
59
+ You can use this tool to get the next train arrival information for a given station and direction.
60
  """
61
+ try:
62
+ feed = NYCTFeed(feed_id)
63
+ trains_data = feed.trips
64
+ except Exception as e:
65
+ return f"Failed to load MTA feed data for feed ID {feed_id}: {e}"
66
+
67
+ if not trains_data:
68
+ return f"No train data found for feed ID {feed_id}."
69
 
70
+ train_info_processed = get_train_info_list(trains_data)
71
+ current_time = datetime.now().strftime("%H:%M:%S")
72
+ train_info_string = f"Current time: {current_time}\n"
73
+ for train in train_info_processed:
74
+ for stop in train["stop_time_updates"]:
75
+ if (
76
+ stop["stop_name"] == target_station
77
+ and train["direction"] == target_direction
78
+ ):
79
+ # train_name = train["name"] # Original notebook had this, but it's often complex like "14:50 S 1 to South Ferry"
80
+ train_line = train["line"]
81
+ # train_direction = train["direction"] # Already have target_direction
82
+ train_arrival = stop["arrival"]
83
+ if train_arrival:
84
+ train_info_string += f"The next {target_direction} bound {train_line} train arriving at {target_station} will arrive at {train_arrival}.\n"
85
+ else:
86
+ train_info_string += f"The next {target_direction} bound {train_line} train is scheduled at {target_station}, but arrival time is not currently available.\n"
87
+ if train_info_string == "":
88
+ return f"No {target_direction} bound trains found for {target_station} on feed {feed_id} at this time."
89
+ return train_info_string
90
 
 
 
 
 
 
 
 
91
 
92
+ if __name__ == "__main__":
93
+ # Initialize and run the server
94
+ mcp.run(transport="stdio")