TMB Bus Arrival Times MCP Server
A Model Context Protocol (MCP) server that provides real-time bus arrival information for TMB (Transports Metropolitans de Barcelona) bus stops.
π Features
Real-time bus arrivals: Get up-to-date information about when buses will arrive
Multiple lines support: See all bus lines serving a stop
Time in minutes: Arrival times shown in minutes for easy understanding
Detailed information: Line numbers, destinations, directions, and bus IDs
MCP compliant: Works with any MCP client including Claude Desktop
π Prerequisites
Node.js 18+
npm or yarn
TMB API credentials (get them at https://developer.tmb.cat/)
π§ Installation
Clone or create the project:
Install dependencies:
Set up your TMB API credentials:
Build the project:
π― Usage
Option 1: Using the Client (Testing)
The client is perfect for testing and development:
Example output:
Option 2: Using with Claude Desktop
Add the server to your Claude Desktop configuration:
macOS: ~/Library/Application Support/Claude/claude_desktop_config.json
Windows: %APPDATA%\Claude\claude_desktop_config.json
Then restart Claude Desktop. You can now ask Claude things like:
"What buses are coming to stop 2775?"
"When is the next bus at Pl Espanya?"
"Show me all bus arrivals for stop 108"
Option 3: Using the Host (Standalone)
Run the server independently:
π οΈ Architecture Explained
MCP Server (src/server.ts)
The core component that:
Exposes the
get_bus_arrivalstool via MCP protocolHandles communication with TMB's iBus API
Transforms raw API responses into user-friendly formats
Runs on stdio transport for easy integration
Key responsibilities:
Tool registration and discovery
Request validation
API communication with error handling
Data transformation and formatting
Host Application (src/host.ts)
A simple wrapper that:
Starts the MCP server as a child process
Manages environment variables
Handles graceful shutdown
When to use: For standalone deployment or testing without an MCP client.
Client Application (src/client.ts)
A demonstration client that:
Connects to the MCP server via stdio
Lists available tools
Calls tools with parameters
Displays formatted results
When to use: For testing, development, or as a reference implementation.
π API Integration Details
The server connects to TMB's iBus API:
Endpoint: https://api.tmb.cat/v1/itransit/bus/parades/{stopCode}
Parameters:
app_id: Your TMB application IDapp_key: Your TMB application key
Response structure:
π Type Safety
The project uses TypeScript with strict type checking. All API responses are properly typed in src/types.ts:
TMBApiResponse: Raw API response structureBusStopInfo: Transformed, user-friendly formatFormattedBusArrival: Individual line information
π§ͺ Testing Different Bus Stops
Try these Barcelona bus stops:
2775: Random stop108: Pl Espanya - FGC1: Pl Catalunya2554: Sagrada FamΓlia
π How It Works
Client Request: An MCP client (like Claude) calls the
get_bus_arrivalstool with a stop codeServer Processing: The MCP server receives the request via stdio transport
API Query: Server makes an authenticated request to TMB's iBus API
Data Transformation: Raw API response is transformed into a readable format
Time Calculation: Arrival timestamps are converted to "minutes until arrival"
Response: Formatted text is returned to the client
π¦ Error Handling
The server handles various error scenarios:
Missing credentials: Exits with clear error message
Invalid stop code: Returns user-friendly error
API errors: Catches and formats API error responses
Network issues: Handles timeout and connection errors
π Security Notes
Never commit your
.envfile or API credentialsThe
.env.examplefile shows the format without real credentialsCredentials are passed via environment variables, not hardcoded
Use MCP's built-in transport security for production deployments
π MCP Protocol Details
This server implements MCP (Model Context Protocol):
Transport: stdio (standard input/output)
Capabilities: Tools
Tool Name:
get_bus_arrivalsInput:
{ stopCode: string }Output: Formatted text with bus arrival information
π€ Contributing
To extend this server:
Add new tools in
server.tsgetTools()methodImplement handlers in
handleToolCall()Update types in
types.tsas neededTest with the client application
π License
MIT
π Credits
Questions? Check the MCP documentation at https://modelcontextprotocol.io/
This server cannot be installed
remote-capable server
The server can be hosted and run remotely because it primarily relies on remote services or has no dependency on the local environment.
Provides real-time bus arrival information for TMB (Transports Metropolitans de Barcelona) bus stops, showing when buses will arrive in minutes with line numbers, destinations, and directions.