-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.js
More file actions
158 lines (132 loc) · 4.56 KB
/
index.js
File metadata and controls
158 lines (132 loc) · 4.56 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
153
154
155
156
157
158
import "dotenv/config";
import path from "path";
const __dirname = import.meta.dirname;
const PORT = process.env.PORT || 8080;
import { openDb } from "gtfs";
import { readFile } from "fs/promises";
import cors from "cors";
import express from "express";
import bodyParser from "body-parser";
import localDb from "./connections/timetableSqliteDb.js";
import notFoundAction from "./actions/notFoundAction.js";
import validateStopCode from "./utils/stopCodeMiddleware.js";
import getClosestStopsAction from "./actions/getClosestStopsAction.js";
import getSingleStopAction from "./actions/getSingleStopAction.js";
import getStopTimetableAction from "./actions/getStopTimetableAction.js";
import getStopStaticDataAction from "./actions/getStopStaticDataAction.js";
import getAllStopsAction from "./actions/getAllStopsAction.js";
import routeInfoDynamicAction from "./actions/routeDynamicInfoAction.js";
import routeInfoStaticAction from "./actions/routeStaticInfoAction.js";
import vehicleInfoAction from "./actions/vehicleInfoAction.js";
import closestTransportAction from "./actions/closestTransportAction.js";
import getAllRoutesAction from "./actions/getAllRoutesAction.js";
import sitemapAction from "./actions/sitemapAction.js";
import {
buildMcpServerCard,
handleMcpPostRequest,
} from "./mcp/timetableMcpServer.js";
const app = express();
app.use(cors());
app.use(bodyParser.json({ limit: "100kb" }));
app.get("/stops/:code/timetable", validateStopCode, getStopTimetableAction);
app.get("/stops/:code/static", validateStopCode, getStopStaticDataAction);
app.get("/stops/:code", validateStopCode, getSingleStopAction);
app.get("/stops.json", getAllStopsAction);
app.get("/stops", getAllStopsAction);
app.get("/closest", getClosestStopsAction);
app.get("/routes.json", getAllRoutesAction);
app.get("/routes", getAllRoutesAction);
app.get("/routes/dynamic/:name", routeInfoDynamicAction);
app.get("/routes/static/:name", routeInfoStaticAction);
app.get("/vehicle/:vehicleId", vehicleInfoAction);
app.get("/transport", closestTransportAction);
app.get("/sitemap.xml", sitemapAction);
app.get("/ping", (req, res) => {
res.json({});
});
app.post("/mcp", async (req, res) => {
try {
await handleMcpPostRequest(req, res);
} catch (error) {
console.error("MCP request handling failed", error);
if (!res.headersSent) {
res.status(500).json({
jsonrpc: "2.0",
error: {
code: -32603,
message: "Internal server error",
},
id: null,
});
}
}
});
app.all("/mcp", (req, res) => {
res.status(405).json({
jsonrpc: "2.0",
error: {
code: -32000,
message: "Method not allowed.",
},
id: null,
});
});
app.get("/.well-known/mcp/server-card.json", (req, res) => {
const baseUrl = `https://${req.get("host")}`;
res.json(buildMcpServerCard(baseUrl));
});
app.get("/mcp-icon.svg", (req, res) => {
res.type("image/svg+xml");
res.set("Cache-Control", "public, max-age=86400");
res.sendFile(path.join(__dirname, "mcp", "mcp-icon.svg"));
});
app.get("/robots.txt", (req, res) => {
const baseUrl = `https://${req.get("host")}`;
const lines = [
"User-agent: *",
"Disallow: /private/",
"",
"# Non-standard hint for AI agent discovery:",
`# mcp-server: ${baseUrl}/.well-known/mcp/server-card.json`,
];
res.type("text/plain").send(lines.join("\n"));
});
app.get("/last-modified.txt", (req, res, next) => {
res.set("Cache-Control", `public, max-age=0, s-maxage=${5 * 60}`);
res.sendFile(path.join(__dirname, "last-modified.txt"));
});
app.get("/favicon.ico", (req, res, next) => {
res
.set("Cache-Control", `public, max-age=0, s-maxage=${3600 * 24 * 31}`)
.set("Cache-Tag", "long");
res.sendFile(path.join(__dirname, "favicon.ico"));
});
app.get("/smithery.json", (req, res) => {
res
.set("Cache-Control", `public, max-age=0, s-maxage=${3600 * 24 * 7}`)
.set("Cache-Tag", "long");
res.sendFile(path.join(__dirname, "smithery.json"));
});
app.get("/server.json", (req, res) => {
res
.set("Cache-Control", `public, max-age=0, s-maxage=${3600 * 24 * 7}`)
.set("Cache-Tag", "long");
res.sendFile(path.join(__dirname, "server.json"));
});
app.use(notFoundAction);
app.use(function (err, req, res, next) {
console.error(err.stack);
res.status(500).send("Something broke!");
});
app.on("ready", () => {
app.listen(PORT, () => {
console.log("Started!");
});
});
localDb.loadDatabase({}, async () => {
const gtfsDbConfig = JSON.parse(
await readFile(new URL("./gtfs-import-config.json", import.meta.url)),
);
await openDb(gtfsDbConfig);
app.emit("ready");
});