mlb-mcp 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +22 -0
- package/dist/api.js +64 -0
- package/dist/index.js +4 -0
- package/dist/server.js +52 -0
- package/package.json +44 -0
- package/server.json +20 -0
package/README.md
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# MLB MCP
|
|
2
|
+
|
|
3
|
+
Teams, schedules, and standings from the official MLB Stats API. No key required.
|
|
4
|
+
|
|
5
|
+
This file is self contained. It reads public data only and never writes to the machine. All output is bounded and honest about what could not be fetched.
|
|
6
|
+
|
|
7
|
+
## Tools
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
* `teams` All MLB teams.
|
|
11
|
+
* `schedule` Games for a date.
|
|
12
|
+
* `standings` Standings for a season.
|
|
13
|
+
|
|
14
|
+
## Usage
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
npm install
|
|
18
|
+
npm run build
|
|
19
|
+
node dist/index.js
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
Data comes from the official public MLB Stats API.
|
package/dist/api.js
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
const BASE = "https://statsapi.mlb.com/api/v1";
|
|
2
|
+
const UA = "mrfentmen-mlb-mcp/1.0 (https://github.com/mrfentmen)";
|
|
3
|
+
export class MlbError extends Error {
|
|
4
|
+
}
|
|
5
|
+
async function get(url) {
|
|
6
|
+
const res = await fetch(url, {
|
|
7
|
+
headers: { "User-Agent": UA, Accept: "application/json" },
|
|
8
|
+
signal: AbortSignal.timeout(30000),
|
|
9
|
+
});
|
|
10
|
+
if (!res.ok)
|
|
11
|
+
throw new MlbError(`MLB Stats API returned HTTP ${res.status}`);
|
|
12
|
+
return (await res.json());
|
|
13
|
+
}
|
|
14
|
+
export async function teams(_args) {
|
|
15
|
+
const d = await get(`${BASE}/teams?sportId=1`);
|
|
16
|
+
const list = (d?.teams ?? []);
|
|
17
|
+
if (!list.length)
|
|
18
|
+
return "No teams found";
|
|
19
|
+
return `MLB teams (${list.length}):\n` + list.map((t, i) => {
|
|
20
|
+
const league = t?.league?.name ?? "";
|
|
21
|
+
const division = t?.division?.name ?? "";
|
|
22
|
+
return `${i + 1}. ${t?.name ?? "n/a"} (${t?.abbreviation ?? ""}) | ${league} ${division ? `| ${division}` : ""}`;
|
|
23
|
+
}).join("\n");
|
|
24
|
+
}
|
|
25
|
+
export async function schedule(args) {
|
|
26
|
+
const date = (args.date ?? new Date().toISOString().slice(0, 10)).trim();
|
|
27
|
+
if (!/^\d{4}-\d{2}-\d{2}$/.test(date))
|
|
28
|
+
throw new MlbError("Provide a date like 2026-08-10");
|
|
29
|
+
const teamId = args.teamId ? Number(args.teamId) : undefined;
|
|
30
|
+
let url = `${BASE}/schedule?sportId=1&date=${date}`;
|
|
31
|
+
if (teamId)
|
|
32
|
+
url += `&teamId=${teamId}`;
|
|
33
|
+
const d = await get(url);
|
|
34
|
+
const games = (d?.dates?.[0]?.games ?? []);
|
|
35
|
+
if (!games.length)
|
|
36
|
+
return `No games on ${date}${teamId ? ` for team ${teamId}` : ""}`;
|
|
37
|
+
return `MLB games on ${date}:\n` + games.map((g, i) => {
|
|
38
|
+
const away = g?.teams?.away?.team?.name ?? "TBD";
|
|
39
|
+
const home = g?.teams?.home?.team?.name ?? "TBD";
|
|
40
|
+
const aR = g?.teams?.away?.score;
|
|
41
|
+
const hR = g?.teams?.home?.score;
|
|
42
|
+
const score = aR != null && hR != null ? ` ${aR}-${hR}` : "";
|
|
43
|
+
const status = g?.status?.detailedState ?? "scheduled";
|
|
44
|
+
return `${i + 1}. ${away} @ ${home}${score} | ${status}`;
|
|
45
|
+
}).join("\n");
|
|
46
|
+
}
|
|
47
|
+
export async function standings(args) {
|
|
48
|
+
const season = Number(args.season ?? new Date().getFullYear());
|
|
49
|
+
const d = await get(`${BASE}/standings?leagueId=103,104&season=${season}`);
|
|
50
|
+
const records = (d?.records ?? []);
|
|
51
|
+
if (!records.length)
|
|
52
|
+
return `No standings for ${season}`;
|
|
53
|
+
const lines = [];
|
|
54
|
+
for (const rec of records) {
|
|
55
|
+
const divName = rec?.division?.name ?? rec?.league?.name ?? "";
|
|
56
|
+
lines.push(`\n${divName}:`);
|
|
57
|
+
const rows = (rec?.teamRecords ?? []).map((tr) => {
|
|
58
|
+
const pct = tr?.pct ?? (tr?.wins && tr?.losses ? (tr.wins / (tr.wins + tr.losses)).toFixed(3) : "-");
|
|
59
|
+
return ` ${String(tr?.rank ?? "").padStart(2)}. ${tr?.team?.name ?? "n/a"} ${tr?.wins ?? 0}-${tr?.losses ?? 0} (${pct})`;
|
|
60
|
+
});
|
|
61
|
+
lines.push(...rows.slice(0, 20));
|
|
62
|
+
}
|
|
63
|
+
return `MLB standings ${season}:` + lines.join("\n");
|
|
64
|
+
}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
|
|
2
|
+
import { createServer } from "./server.js";
|
|
3
|
+
const main = async () => { const server = createServer(); await server.connect(new StdioServerTransport()); };
|
|
4
|
+
main().catch((error) => { console.error("Fatal error:", error); process.exit(1); });
|
package/dist/server.js
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
import { schedule } from "./api.js";
|
|
4
|
+
import { standings } from "./api.js";
|
|
5
|
+
import { teams } from "./api.js";
|
|
6
|
+
const text = (value) => ({ content: [{ type: "text", text: value }] });
|
|
7
|
+
const textError = (t) => ({ content: [{ type: "text", text: t }], isError: true });
|
|
8
|
+
const READ_ONLY = { readOnlyHint: true, openWorldHint: true };
|
|
9
|
+
const error = (e) => `Error: ${e instanceof Error ? e.message : String(e)}`;
|
|
10
|
+
export function createServer() {
|
|
11
|
+
const server = new McpServer({ name: "mlb-mcp", version: "1.0.0" });
|
|
12
|
+
server.registerTool("teams", {
|
|
13
|
+
title: "Teams",
|
|
14
|
+
description: "All MLB teams.",
|
|
15
|
+
inputSchema: z.object({}),
|
|
16
|
+
annotations: READ_ONLY,
|
|
17
|
+
}, async (args) => {
|
|
18
|
+
try {
|
|
19
|
+
return text(await teams(args));
|
|
20
|
+
}
|
|
21
|
+
catch (e) {
|
|
22
|
+
return textError(error(e));
|
|
23
|
+
}
|
|
24
|
+
});
|
|
25
|
+
server.registerTool("schedule", {
|
|
26
|
+
title: "Schedule",
|
|
27
|
+
description: "Games for a date.",
|
|
28
|
+
inputSchema: z.object({ date: z.string().describe("Date like 2026-08-10."), teamId: z.number().describe("Optional team ID.").optional() }),
|
|
29
|
+
annotations: READ_ONLY,
|
|
30
|
+
}, async (args) => {
|
|
31
|
+
try {
|
|
32
|
+
return text(await schedule(args));
|
|
33
|
+
}
|
|
34
|
+
catch (e) {
|
|
35
|
+
return textError(error(e));
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
server.registerTool("standings", {
|
|
39
|
+
title: "Standings",
|
|
40
|
+
description: "Standings for a season.",
|
|
41
|
+
inputSchema: z.object({ season: z.number().describe("Season year.").optional() }),
|
|
42
|
+
annotations: READ_ONLY,
|
|
43
|
+
}, async (args) => {
|
|
44
|
+
try {
|
|
45
|
+
return text(await standings(args));
|
|
46
|
+
}
|
|
47
|
+
catch (e) {
|
|
48
|
+
return textError(error(e));
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
return server;
|
|
52
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": "1.0.0",
|
|
3
|
+
"type": "module",
|
|
4
|
+
"repository": {
|
|
5
|
+
"type": "git",
|
|
6
|
+
"url": "https://github.com/mrfentmen/mlb-mcp.git"
|
|
7
|
+
},
|
|
8
|
+
"bin": {
|
|
9
|
+
"mlb-mcp": "./dist/index.js"
|
|
10
|
+
},
|
|
11
|
+
"main": "./dist/index.js",
|
|
12
|
+
"files": [
|
|
13
|
+
"dist",
|
|
14
|
+
"server.json",
|
|
15
|
+
"README.md"
|
|
16
|
+
],
|
|
17
|
+
"scripts": {
|
|
18
|
+
"build": "tsc -p tsconfig.json",
|
|
19
|
+
"start": "node dist/index.js",
|
|
20
|
+
"dev": "npm run build && node dist/index.js"
|
|
21
|
+
},
|
|
22
|
+
"license": "MIT",
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"@modelcontextprotocol/sdk": "^1.0.4",
|
|
25
|
+
"zod": "^3.23.8"
|
|
26
|
+
},
|
|
27
|
+
"devDependencies": {
|
|
28
|
+
"@types/node": "^22.0.0",
|
|
29
|
+
"typescript": "^5.6.0"
|
|
30
|
+
},
|
|
31
|
+
"name": "mlb-mcp",
|
|
32
|
+
"description": "Teams, schedules, and standings from the official MLB Stats API. No key required.",
|
|
33
|
+
"mcpName": "io.github.mrfentmen/mlb-mcp",
|
|
34
|
+
"keywords": [
|
|
35
|
+
"mcp",
|
|
36
|
+
"mlb",
|
|
37
|
+
"baseball",
|
|
38
|
+
"sports",
|
|
39
|
+
"stats"
|
|
40
|
+
],
|
|
41
|
+
"engines": {
|
|
42
|
+
"node": ">=20"
|
|
43
|
+
}
|
|
44
|
+
}
|
package/server.json
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "https://static.modelcontextprotocol.io/schemas/2025-12-11/server.schema.json",
|
|
3
|
+
"name": "io.github.mrfentmen/mlb-mcp",
|
|
4
|
+
"description": "Teams, schedules, and standings from the official MLB Stats API. No key required.",
|
|
5
|
+
"repository": {
|
|
6
|
+
"url": "https://github.com/mrfentmen/mlb-mcp",
|
|
7
|
+
"source": "github"
|
|
8
|
+
},
|
|
9
|
+
"version": "1.0.0",
|
|
10
|
+
"packages": [
|
|
11
|
+
{
|
|
12
|
+
"registryType": "npm",
|
|
13
|
+
"identifier": "mlb-mcp",
|
|
14
|
+
"version": "1.0.0",
|
|
15
|
+
"transport": {
|
|
16
|
+
"type": "stdio"
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
]
|
|
20
|
+
}
|