From da45941b505af4b1de61a1a12a733c1aa4cbcd09 Mon Sep 17 00:00:00 2001 From: Mathieu Picciolli Date: Mon, 29 Jun 2026 07:12:10 -0400 Subject: [PATCH 1/3] feat: add pcm_get_team_roster tool to list team rosters from PCM save files --- README.md | 1 + src/tools/get-team-roster.ts | 180 +++++++++++++++++++++++++++++++++++ src/tools/index.ts | 2 + 3 files changed, 183 insertions(+) create mode 100644 src/tools/get-team-roster.ts diff --git a/README.md b/README.md index 171a073..4d10e23 100644 --- a/README.md +++ b/README.md @@ -83,6 +83,7 @@ All tools are read-only and carry `readOnlyHint: true`, so clients like Claude D | **pcm_get_table_schema** | Inspect a single table by name. Returns its columns (name, SQL type, NOT NULL and primary key flags) and its row count. Use `pcm_get_save_schema` first to discover available table names. | | **pcm_get_player_info** | Get the active human player and their team from a save file. Returns the player login plus team details (name, resolved division name, resolved country name, evaluation and manager). | | **pcm_search_cyclist** | Search for a cyclist by first name and/or last name (case-insensitive partial match). Returns up to 10 matches with all ratings (plain, mountain, medium mountain, downhilling, cobble, time trial, prologue, sprint, acceleration, endurance, resistance, recuperation, hill, baroudeur, current ability) and the resolved country name. `mediumMountain` and `currentAbility` are `null` on saves that pre-date those columns. | +| **pcm_get_team_roster** | List a team's roster (defaults to the active player's team when `teamId` is omitted). Joins DYN_cyclist with its active DYN_contract_cyclist and STA_type_rider; per rider returns name, age (derived from birth date and the current game date), rider type, overall ability, contract end year, wage and market value. Ordered by overall ability, highest first. | | **pcm_query_save** | Run a read-only SQL query (`SELECT` / `WITH … SELECT` only) against any table in a save file. Write/DDL statements are rejected. Results are capped (default 100, max 1000 rows). | ## Development diff --git a/src/tools/get-team-roster.ts b/src/tools/get-team-roster.ts new file mode 100644 index 0000000..8753509 --- /dev/null +++ b/src/tools/get-team-roster.ts @@ -0,0 +1,180 @@ +import type { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js"; +import { z } from "zod"; +import { withSaveDb } from "../save-db"; + +const riderSchema = z.object({ + id: z.number().describe("Cyclist ID (IDcyclist)"), + firstName: z.string().describe("First name (gene_sz_firstname)"), + lastName: z.string().describe("Last name (gene_sz_lastname)"), + age: z + .number() + .nullable() + .describe( + "Age in years, derived from gene_i_birthdate and the current game date (GAM_config.gene_i_date). Null when the birth date is missing.", + ), + type: z + .string() + .nullable() + .describe( + "Rider type key (STA_type_rider.CONSTANT via fkIDtype_rider), e.g. sprint, mountain, tour, timetrial, flat, ardennaises, flandriennes. Null when unset.", + ), + overall: z + .number() + .nullable() + .describe( + "Overall ability / note globale (value_f_current_ability) — null on saves that pre-date this column.", + ), + contractEndYear: z + .number() + .nullable() + .describe( + "Year the active contract ends (DYN_contract_cyclist.iYearEnd). Null when no active contract is found.", + ), + wage: z + .number() + .nullable() + .describe( + "Salary for the contract period (DYN_contract_cyclist.finan_i_period_wage). Null when no active contract is found.", + ), + value: z + .number() + .nullable() + .describe( + "Market value / valeur (value_f_capital) — null on saves that pre-date this column.", + ), +}); + +const outputSchema = z.object({ + teamId: z.number().describe("Team ID the roster belongs to (IDteam)"), + count: z.number().describe("Number of cyclists in the roster"), + riders: z + .array(riderSchema) + .describe("Roster cyclists, ordered by overall ability (highest first)"), +}); + +/** Compute age in whole years from two YYYYMMDD integers (e.g. 20030503). */ +function ageFromYmd(currentYmd: number, birthYmd: number): number { + let age = Math.floor(currentYmd / 10000) - Math.floor(birthYmd / 10000); + // Decrement if this year's birthday (MMDD) has not occurred yet. + if (currentYmd % 10000 < birthYmd % 10000) { + age--; + } + return age; +} + +export function registerGetTeamRoster(server: McpServer): void { + server.registerTool( + "pcm_get_team_roster", + { + title: "Get PCM team roster", + description: + "List the roster of a team in a Pro Cycling Manager `.cdb` save file. Defaults to the active human player's team (GAM_user.game_i_active = 1) when `teamId` is omitted. Joins DYN_cyclist with its active DYN_contract_cyclist and STA_type_rider, and for each rider returns name, age, rider type, overall ability (note globale), contract end year, wage and market value. Ordered by overall ability, highest first.", + inputSchema: { + savePath: z.string().describe("Absolute path to the .cdb save file"), + teamId: z + .number() + .int() + .optional() + .describe( + "Team ID (IDteam) whose roster to list. Defaults to the active player's team when omitted.", + ), + }, + outputSchema, + annotations: { + readOnlyHint: true, + destructiveHint: false, + idempotentHint: true, + openWorldHint: false, + }, + }, + async ({ savePath, teamId }) => + withSaveDb(savePath, (db, save) => { + // Resolve the target team: explicit teamId, else the active player's team. + let resolvedTeamId = teamId; + if (resolvedTeamId == null) { + const teamResult = db.exec( + "SELECT fkIDteam_duplicate FROM GAM_user WHERE game_i_active = 1", + ); + const value = teamResult[0]?.values?.[0]?.[0]; + if (value == null) { + throw new Error( + `No active player (game_i_active = 1) found in ${save.name}; pass teamId explicitly.`, + ); + } + resolvedTeamId = Number(value); + } + + // The current in-game date (YYYYMMDD) is the reference point for age. + const dateResult = db.exec( + "SELECT gene_i_date FROM GAM_config LIMIT 1", + ); + const currentYmdRaw = dateResult[0]?.values?.[0]?.[0]; + const currentYmd = + currentYmdRaw != null ? Number(currentYmdRaw) : null; + + // Some columns are absent on saves that pre-date them — detect them so + // the query stays valid across PCM versions. + const columnInfo = db.exec(`PRAGMA table_info("DYN_cyclist")`); + const columnNames = new Set( + (columnInfo[0]?.values ?? []).map((r) => String(r[1])), + ); + const hasCurrentAbility = columnNames.has("value_f_current_ability"); + const hasCapital = columnNames.has("value_f_capital"); + + const stmt = db.prepare( + `SELECT + c.IDcyclist AS id, + c.gene_sz_firstname AS firstName, + c.gene_sz_lastname AS lastName, + c.gene_i_birthdate AS birthdate, + ${hasCurrentAbility ? "c.value_f_current_ability" : "NULL"} AS overall, + ${hasCapital ? "c.value_f_capital" : "NULL"} AS value, + tr.CONSTANT AS type, + ct.iYearEnd AS contractEndYear, + ct.finan_i_period_wage AS wage + FROM DYN_cyclist c + LEFT JOIN STA_type_rider tr ON c.fkIDtype_rider = tr.IDtype_rider + LEFT JOIN DYN_contract_cyclist ct + ON ct.fkIDcyclist = c.IDcyclist AND ct.gene_b_active_contract = 1 + WHERE c.fkIDteam = :teamId + ORDER BY overall DESC, c.gene_sz_lastname ASC`, + ); + + const riders: z.infer[] = []; + try { + stmt.bind({ ":teamId": resolvedTeamId }); + while (stmt.step()) { + const row = stmt.getAsObject(); + const birthdate = + row.birthdate != null ? Number(row.birthdate) : null; + riders.push({ + id: Number(row.id), + firstName: String(row.firstName), + lastName: String(row.lastName), + age: + currentYmd != null && birthdate != null + ? ageFromYmd(currentYmd, birthdate) + : null, + type: row.type != null ? String(row.type) : null, + overall: row.overall != null ? Number(row.overall) : null, + contractEndYear: + row.contractEndYear != null + ? Number(row.contractEndYear) + : null, + wage: row.wage != null ? Number(row.wage) : null, + value: row.value != null ? Number(row.value) : null, + }); + } + } finally { + stmt.free(); + } + + const output: z.infer = { + teamId: resolvedTeamId, + count: riders.length, + riders, + }; + return output; + }), + ); +} diff --git a/src/tools/index.ts b/src/tools/index.ts index f3a4fea..666e744 100644 --- a/src/tools/index.ts +++ b/src/tools/index.ts @@ -4,6 +4,7 @@ import { registerSelectSave } from "./select-save"; import { registerGetSaveSchema } from "./get-save-schema"; import { registerGetTableSchema } from "./get-table-schema"; import { registerGetPlayerInfo } from "./get-player-info"; +import { registerGetTeamRoster } from "./get-team-roster"; import { registerQuerySave } from "./query-save"; import { registerSearchCyclist } from "./search-cyclist"; @@ -13,6 +14,7 @@ export function registerTools(server: McpServer): void { registerGetSaveSchema(server); registerGetTableSchema(server); registerGetPlayerInfo(server); + registerGetTeamRoster(server); registerQuerySave(server); registerSearchCyclist(server); } From 447c489d8f296e8b1f4be161b1d179da7296a27b Mon Sep 17 00:00:00 2001 From: Mathieu Picciolli Date: Mon, 29 Jun 2026 08:05:23 -0400 Subject: [PATCH 2/3] feat: enhance team roster tool with country and per-terrain ratings; add age calculation utility --- AGENTS.md | 8 +++- README.md | 2 +- src/helpers.ts | 10 +++++ src/save-db.ts | 16 +++++++- src/schemas/cyclist.ts | 79 ++++++++++++++++++++++++++++++++++++ src/tools/get-team-roster.ts | 65 +++++++++++++++++------------ src/tools/search-cyclist.ts | 56 ++----------------------- test/helpers.test.ts | 24 ++++++++++- 8 files changed, 176 insertions(+), 84 deletions(-) create mode 100644 src/schemas/cyclist.ts diff --git a/AGENTS.md b/AGENTS.md index ed138c4..84fdd11 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -27,8 +27,10 @@ read-only guarantee. src/ index.ts # entrypoint: builds McpServer, registers tools, connects stdio saves.ts # save discovery + validation (listSaves, validateSave, getPcmRoot) - save-db.ts # withSaveDb(): open .cdb in-memory, run fn, always close db - helpers.ts # validResponse / errorResponse → CallToolResult + save-db.ts # withSaveDb(): open .cdb in-memory, run fn, always close db; getGameDate() + helpers.ts # validResponse / errorResponse → CallToolResult; ageFromYmd() + schemas/ + cyclist.ts # shared cyclist ratings: ratingsSchema / ratingsColumns() / mapRatings() tools/ index.ts # registerTools() — wires every tool onto the server list-saves.ts # pcm_list_saves @@ -36,6 +38,7 @@ src/ get-save-schema.ts # pcm_get_save_schema get-table-schema.ts # pcm_get_table_schema get-player-info.ts # pcm_get_player_info + get-team-roster.ts # pcm_get_team_roster search-cyclist.ts # pcm_search_cyclist query-save.ts # pcm_query_save test/ # vitest specs (test/**/*.test.ts) @@ -52,6 +55,7 @@ All tools are prefixed with `pcm_` and carry `readOnlyHint: true` / `destructive | `pcm_get_save_schema` | List all tables (id + name) in a save via `DB_STRUCTURE`. | | `pcm_get_table_schema` | Inspect one table: columns (name, type, NOT NULL, PK) + row count. | | `pcm_get_player_info` | Active human player + team (joins `GAM_user` `game_i_active = 1` with `DYN_team`). | +| `pcm_get_team_roster` | Team roster (defaults to active player's team). Joins `DYN_cyclist` with active `DYN_contract_cyclist` + `STA_type_rider`: name, country, age, type, overall, contract end, wage, value, plus per-terrain ratings (flat). Errors on unknown `teamId`. | | `pcm_search_cyclist` | Search cyclist by first/last name (partial, case-insensitive). | | `pcm_query_save` | Run a single read-only `SELECT`/`WITH … SELECT`. Write/DDL rejected; results capped (default 100, max 1000). | diff --git a/README.md b/README.md index 4d10e23..9ce3452 100644 --- a/README.md +++ b/README.md @@ -83,7 +83,7 @@ All tools are read-only and carry `readOnlyHint: true`, so clients like Claude D | **pcm_get_table_schema** | Inspect a single table by name. Returns its columns (name, SQL type, NOT NULL and primary key flags) and its row count. Use `pcm_get_save_schema` first to discover available table names. | | **pcm_get_player_info** | Get the active human player and their team from a save file. Returns the player login plus team details (name, resolved division name, resolved country name, evaluation and manager). | | **pcm_search_cyclist** | Search for a cyclist by first name and/or last name (case-insensitive partial match). Returns up to 10 matches with all ratings (plain, mountain, medium mountain, downhilling, cobble, time trial, prologue, sprint, acceleration, endurance, resistance, recuperation, hill, baroudeur, current ability) and the resolved country name. `mediumMountain` and `currentAbility` are `null` on saves that pre-date those columns. | -| **pcm_get_team_roster** | List a team's roster (defaults to the active player's team when `teamId` is omitted). Joins DYN_cyclist with its active DYN_contract_cyclist and STA_type_rider; per rider returns name, age (derived from birth date and the current game date), rider type, overall ability, contract end year, wage and market value. Ordered by overall ability, highest first. | +| **pcm_get_team_roster** | List a team's roster (defaults to the active player's team when `teamId` is omitted). Joins DYN_cyclist with its active DYN_contract_cyclist and STA_type_rider; per rider returns name, country, age (derived from birth date and the current game date), rider type, overall ability, contract end year, wage, market value and all per-terrain ability ratings. Ordered by overall ability, highest first. Errors if `teamId` does not exist. | | **pcm_query_save** | Run a read-only SQL query (`SELECT` / `WITH … SELECT` only) against any table in a save file. Write/DDL statements are rejected. Results are capped (default 100, max 1000 rows). | ## Development diff --git a/src/helpers.ts b/src/helpers.ts index 14197f5..d3d9e4f 100644 --- a/src/helpers.ts +++ b/src/helpers.ts @@ -29,3 +29,13 @@ export function errorResponse(error: string): CallToolResult { isError: true, }; } + +/** Compute age in whole years from two YYYYMMDD integers (e.g. 20030503). */ +export function ageFromYmd(currentYmd: number, birthYmd: number): number { + let age = Math.floor(currentYmd / 10000) - Math.floor(birthYmd / 10000); + // Decrement if this year's birthday (MMDD) has not occurred yet. + if (currentYmd % 10000 < birthYmd % 10000) { + age--; + } + return age; +} diff --git a/src/save-db.ts b/src/save-db.ts index 461b018..2b18877 100644 --- a/src/save-db.ts +++ b/src/save-db.ts @@ -6,7 +6,21 @@ import { errorResponse, validResponse } from "./helpers"; import { type SaveFile, validateSave } from "./saves"; /** An in-memory sql.js database produced from a `.cdb` save by `cdbToSql`. */ -type SaveDb = ReturnType; +export type SaveDb = ReturnType; + +/** + * Read the current in-game date from a save as a `YYYYMMDD` integer + * (e.g. `20260605`), or `null` when it can't be found. + * + * PCM stores the career's current date in `GAM_config.gene_i_date`. It is the + * reference point for any age- or season-relative computation, since the + * on-disk save advances as the career is played. + */ +export function getGameDate(db: SaveDb): number | null { + const result = db.exec("SELECT gene_i_date FROM GAM_config LIMIT 1"); + const raw = result[0]?.values?.[0]?.[0]; + return raw != null ? Number(raw) : null; +} /** * Open a Pro Cycling Manager `.cdb` save as an in-memory SQL database, run diff --git a/src/schemas/cyclist.ts b/src/schemas/cyclist.ts new file mode 100644 index 0000000..755b217 --- /dev/null +++ b/src/schemas/cyclist.ts @@ -0,0 +1,79 @@ +import { z } from "zod"; + +/** + * Per-terrain ability ratings shared by every tool that returns a cyclist + * (`pcm_search_cyclist`, `pcm_get_team_roster`). These map one-to-one to the + * `charac_i_*` columns on `DYN_cyclist`. + * + * Spread `ratingsSchema.shape` into a cyclist's output schema to keep the + * ratings flat, and use {@link mapRatings} to read them off a result row (both + * expect the columns to be aliased to the field names below). + */ +export const ratingsSchema = z.object({ + plain: z.number().describe("Plain rating (charac_i_plain)"), + mountain: z.number().describe("Mountain rating (charac_i_mountain)"), + mediumMountain: z + .number() + .nullable() + .describe( + "Medium mountain rating (charac_i_medium_mountain) — null on saves that pre-date this column", + ), + downhilling: z.number().describe("Downhilling rating (charac_i_downhilling)"), + cobble: z.number().describe("Cobblestone rating (charac_i_cobble)"), + timeTrial: z.number().describe("Time trial rating (charac_i_timetrial)"), + prologue: z.number().describe("Prologue rating (charac_i_prologue)"), + sprint: z.number().describe("Sprint rating (charac_i_sprint)"), + acceleration: z + .number() + .describe("Acceleration rating (charac_i_acceleration)"), + endurance: z.number().describe("Endurance rating (charac_i_endurance)"), + resistance: z.number().describe("Resistance rating (charac_i_resistance)"), + recuperation: z + .number() + .describe("Recuperation rating (charac_i_recuperation)"), + hill: z.number().describe("Hill rating (charac_i_hill)"), + baroudeur: z.number().describe("Baroudeur rating (charac_i_baroudeur)"), +}); + +/** SQL `SELECT` fragment that aliases the rating columns to {@link ratingsSchema}'s + * field names. `mediumMountain` falls back to `NULL` on saves that pre-date the + * `charac_i_medium_mountain` column. */ +export function ratingsColumns(hasMediumMountain: boolean): string { + return `c.charac_i_plain AS plain, + c.charac_i_mountain AS mountain, + ${hasMediumMountain ? "c.charac_i_medium_mountain" : "NULL"} AS mediumMountain, + c.charac_i_downhilling AS downhilling, + c.charac_i_cobble AS cobble, + c.charac_i_timetrial AS timeTrial, + c.charac_i_prologue AS prologue, + c.charac_i_sprint AS sprint, + c.charac_i_acceleration AS acceleration, + c.charac_i_endurance AS endurance, + c.charac_i_resistance AS resistance, + c.charac_i_recuperation AS recuperation, + c.charac_i_hill AS hill, + c.charac_i_baroudeur AS baroudeur`; +} + +/** Read the rating fields off a query row aliased per {@link ratingsColumns}. */ +export function mapRatings( + row: Record, +): z.infer { + return { + plain: Number(row.plain), + mountain: Number(row.mountain), + mediumMountain: + row.mediumMountain != null ? Number(row.mediumMountain) : null, + downhilling: Number(row.downhilling), + cobble: Number(row.cobble), + timeTrial: Number(row.timeTrial), + prologue: Number(row.prologue), + sprint: Number(row.sprint), + acceleration: Number(row.acceleration), + endurance: Number(row.endurance), + resistance: Number(row.resistance), + recuperation: Number(row.recuperation), + hill: Number(row.hill), + baroudeur: Number(row.baroudeur), + }; +} diff --git a/src/tools/get-team-roster.ts b/src/tools/get-team-roster.ts index 8753509..5c18988 100644 --- a/src/tools/get-team-roster.ts +++ b/src/tools/get-team-roster.ts @@ -1,11 +1,17 @@ import type { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js"; import { z } from "zod"; -import { withSaveDb } from "../save-db"; +import { mapRatings, ratingsColumns, ratingsSchema } from "../schemas/cyclist"; +import { ageFromYmd } from "../helpers"; +import { getGameDate, withSaveDb } from "../save-db"; -const riderSchema = z.object({ +const cyclistSchema = z.object({ id: z.number().describe("Cyclist ID (IDcyclist)"), firstName: z.string().describe("First name (gene_sz_firstname)"), lastName: z.string().describe("Last name (gene_sz_lastname)"), + country: z + .string() + .nullable() + .describe("Country name (STA_country.CONSTANT)"), age: z .number() .nullable() @@ -42,33 +48,23 @@ const riderSchema = z.object({ .describe( "Market value / valeur (value_f_capital) — null on saves that pre-date this column.", ), + ...ratingsSchema.shape, }); const outputSchema = z.object({ teamId: z.number().describe("Team ID the roster belongs to (IDteam)"), - count: z.number().describe("Number of cyclists in the roster"), - riders: z - .array(riderSchema) + cyclists: z + .array(cyclistSchema) .describe("Roster cyclists, ordered by overall ability (highest first)"), }); -/** Compute age in whole years from two YYYYMMDD integers (e.g. 20030503). */ -function ageFromYmd(currentYmd: number, birthYmd: number): number { - let age = Math.floor(currentYmd / 10000) - Math.floor(birthYmd / 10000); - // Decrement if this year's birthday (MMDD) has not occurred yet. - if (currentYmd % 10000 < birthYmd % 10000) { - age--; - } - return age; -} - export function registerGetTeamRoster(server: McpServer): void { server.registerTool( "pcm_get_team_roster", { title: "Get PCM team roster", description: - "List the roster of a team in a Pro Cycling Manager `.cdb` save file. Defaults to the active human player's team (GAM_user.game_i_active = 1) when `teamId` is omitted. Joins DYN_cyclist with its active DYN_contract_cyclist and STA_type_rider, and for each rider returns name, age, rider type, overall ability (note globale), contract end year, wage and market value. Ordered by overall ability, highest first.", + "List the roster of a team in a Pro Cycling Manager `.cdb` save file. Defaults to the active human player's team (GAM_user.game_i_active = 1) when `teamId` is omitted. Joins DYN_cyclist with its active DYN_contract_cyclist and STA_type_rider, and for each rider returns name, country, age, rider type, overall ability (note globale), contract end year, wage, market value and all per-terrain ability ratings (plain, mountain, medium mountain, downhilling, cobble, time trial, prologue, sprint, acceleration, endurance, resistance, recuperation, hill, baroudeur). Ordered by overall ability, highest first.", inputSchema: { savePath: z.string().describe("Absolute path to the .cdb save file"), teamId: z @@ -104,13 +100,22 @@ export function registerGetTeamRoster(server: McpServer): void { resolvedTeamId = Number(value); } - // The current in-game date (YYYYMMDD) is the reference point for age. - const dateResult = db.exec( - "SELECT gene_i_date FROM GAM_config LIMIT 1", + const teamStmt = db.prepare( + "SELECT 1 FROM DYN_team WHERE IDteam = :teamId LIMIT 1", ); - const currentYmdRaw = dateResult[0]?.values?.[0]?.[0]; - const currentYmd = - currentYmdRaw != null ? Number(currentYmdRaw) : null; + try { + teamStmt.bind({ ":teamId": resolvedTeamId }); + if (!teamStmt.step()) { + throw new Error( + `Team ${resolvedTeamId} not found in ${save.name}.`, + ); + } + } finally { + teamStmt.free(); + } + + // The current in-game date (YYYYMMDD) is the reference point for age. + const currentYmd = getGameDate(db); // Some columns are absent on saves that pre-date them — detect them so // the query stays valid across PCM versions. @@ -120,6 +125,7 @@ export function registerGetTeamRoster(server: McpServer): void { ); const hasCurrentAbility = columnNames.has("value_f_current_ability"); const hasCapital = columnNames.has("value_f_capital"); + const hasMediumMountain = columnNames.has("charac_i_medium_mountain"); const stmt = db.prepare( `SELECT @@ -131,26 +137,31 @@ export function registerGetTeamRoster(server: McpServer): void { ${hasCapital ? "c.value_f_capital" : "NULL"} AS value, tr.CONSTANT AS type, ct.iYearEnd AS contractEndYear, - ct.finan_i_period_wage AS wage + ct.finan_i_period_wage AS wage, + co.CONSTANT AS country, + ${ratingsColumns(hasMediumMountain)} FROM DYN_cyclist c LEFT JOIN STA_type_rider tr ON c.fkIDtype_rider = tr.IDtype_rider + LEFT JOIN STA_region r ON c.fkIDregion = r.IDregion + LEFT JOIN STA_country co ON r.fkIDcountry = co.IDcountry LEFT JOIN DYN_contract_cyclist ct ON ct.fkIDcyclist = c.IDcyclist AND ct.gene_b_active_contract = 1 WHERE c.fkIDteam = :teamId ORDER BY overall DESC, c.gene_sz_lastname ASC`, ); - const riders: z.infer[] = []; + const cyclists: z.infer[] = []; try { stmt.bind({ ":teamId": resolvedTeamId }); while (stmt.step()) { const row = stmt.getAsObject(); const birthdate = row.birthdate != null ? Number(row.birthdate) : null; - riders.push({ + cyclists.push({ id: Number(row.id), firstName: String(row.firstName), lastName: String(row.lastName), + country: row.country != null ? String(row.country) : null, age: currentYmd != null && birthdate != null ? ageFromYmd(currentYmd, birthdate) @@ -163,6 +174,7 @@ export function registerGetTeamRoster(server: McpServer): void { : null, wage: row.wage != null ? Number(row.wage) : null, value: row.value != null ? Number(row.value) : null, + ...mapRatings(row), }); } } finally { @@ -171,8 +183,7 @@ export function registerGetTeamRoster(server: McpServer): void { const output: z.infer = { teamId: resolvedTeamId, - count: riders.length, - riders, + cyclists, }; return output; }), diff --git a/src/tools/search-cyclist.ts b/src/tools/search-cyclist.ts index f44b36e..9bbf1c5 100644 --- a/src/tools/search-cyclist.ts +++ b/src/tools/search-cyclist.ts @@ -1,5 +1,6 @@ import type { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js"; import { z } from "zod"; +import { mapRatings, ratingsColumns, ratingsSchema } from "../schemas/cyclist"; import { withSaveDb } from "../save-db"; const cyclistSchema = z.object({ @@ -10,29 +11,7 @@ const cyclistSchema = z.object({ .string() .nullable() .describe("Country name (STA_country.CONSTANT)"), - plain: z.number().describe("Plain rating (charac_i_plain)"), - mountain: z.number().describe("Mountain rating (charac_i_mountain)"), - mediumMountain: z - .number() - .nullable() - .describe( - "Medium mountain rating (charac_i_medium_mountain) — null on saves that pre-date this column", - ), - downhilling: z.number().describe("Downhilling rating (charac_i_downhilling)"), - cobble: z.number().describe("Cobblestone rating (charac_i_cobble)"), - timeTrial: z.number().describe("Time trial rating (charac_i_timetrial)"), - prologue: z.number().describe("Prologue rating (charac_i_prologue)"), - sprint: z.number().describe("Sprint rating (charac_i_sprint)"), - acceleration: z - .number() - .describe("Acceleration rating (charac_i_acceleration)"), - endurance: z.number().describe("Endurance rating (charac_i_endurance)"), - resistance: z.number().describe("Resistance rating (charac_i_resistance)"), - recuperation: z - .number() - .describe("Recuperation rating (charac_i_recuperation)"), - hill: z.number().describe("Hill rating (charac_i_hill)"), - baroudeur: z.number().describe("Baroudeur rating (charac_i_baroudeur)"), + ...ratingsSchema.shape, currentAbility: z .number() .nullable() @@ -89,20 +68,7 @@ export function registerSearchCyclist(server: McpServer): void { c.IDcyclist, c.gene_sz_firstname, c.gene_sz_lastname, - c.charac_i_plain AS plain, - c.charac_i_mountain AS mountain, - ${hasMediumMountain ? "c.charac_i_medium_mountain" : "NULL"} AS mediumMountain, - c.charac_i_downhilling AS downhilling, - c.charac_i_cobble AS cobble, - c.charac_i_timetrial AS timeTrial, - c.charac_i_prologue AS prologue, - c.charac_i_sprint AS sprint, - c.charac_i_acceleration AS acceleration, - c.charac_i_endurance AS endurance, - c.charac_i_resistance AS resistance, - c.charac_i_recuperation AS recuperation, - c.charac_i_hill AS hill, - c.charac_i_baroudeur AS baroudeur, + ${ratingsColumns(hasMediumMountain)}, ${hasCurrentAbility ? "c.value_f_current_ability" : "NULL"} AS currentAbility, co.CONSTANT AS country FROM DYN_cyclist c @@ -133,21 +99,7 @@ export function registerSearchCyclist(server: McpServer): void { firstName: String(row.gene_sz_firstname), lastName: String(row.gene_sz_lastname), country: row.country != null ? String(row.country) : null, - plain: Number(row.plain), - mountain: Number(row.mountain), - mediumMountain: - row.mediumMountain != null ? Number(row.mediumMountain) : null, - downhilling: Number(row.downhilling), - cobble: Number(row.cobble), - timeTrial: Number(row.timeTrial), - prologue: Number(row.prologue), - sprint: Number(row.sprint), - acceleration: Number(row.acceleration), - endurance: Number(row.endurance), - resistance: Number(row.resistance), - recuperation: Number(row.recuperation), - hill: Number(row.hill), - baroudeur: Number(row.baroudeur), + ...mapRatings(row), currentAbility: row.currentAbility != null ? Number(row.currentAbility) : null, }); diff --git a/test/helpers.test.ts b/test/helpers.test.ts index 190134a..53ca281 100644 --- a/test/helpers.test.ts +++ b/test/helpers.test.ts @@ -1,5 +1,5 @@ import { describe, expect, it } from "vitest"; -import { errorResponse, validResponse } from "../src/helpers"; +import { ageFromYmd, errorResponse, validResponse } from "../src/helpers"; describe("validResponse", () => { it("wraps structured content as pretty-printed JSON text", () => { @@ -23,6 +23,28 @@ describe("validResponse", () => { }); }); +describe("ageFromYmd", () => { + it("computes age when this year's birthday has already passed", () => { + // born 2003-05-03, current 2026-06-05 → 23rd birthday already passed + expect(ageFromYmd(20260605, 20030503)).toBe(23); + }); + + it("subtracts a year when this year's birthday has not occurred yet", () => { + // born 2003-07-20, current 2026-06-05 → still 22 until July + expect(ageFromYmd(20260605, 20030720)).toBe(22); + }); + + it("counts the birthday itself as a full year", () => { + // born 2003-06-05, current 2026-06-05 → exactly 23 on the day + expect(ageFromYmd(20260605, 20030605)).toBe(23); + }); + + it("treats the day before the birthday as the younger age", () => { + // born 2003-06-05, current 2026-06-04 → still 22, one day short + expect(ageFromYmd(20260604, 20030605)).toBe(22); + }); +}); + describe("errorResponse", () => { it("flags the response as an error and echoes the message", () => { const result = errorResponse("Save file not found"); From 11b4405cda0c94e3b3e061595e084e759027a958 Mon Sep 17 00:00:00 2001 From: Mathieu Picciolli Date: Mon, 29 Jun 2026 08:51:21 -0400 Subject: [PATCH 3/3] Added try catch Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --- src/save-db.ts | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/save-db.ts b/src/save-db.ts index 2b18877..d9a68c2 100644 --- a/src/save-db.ts +++ b/src/save-db.ts @@ -17,9 +17,13 @@ export type SaveDb = ReturnType; * on-disk save advances as the career is played. */ export function getGameDate(db: SaveDb): number | null { - const result = db.exec("SELECT gene_i_date FROM GAM_config LIMIT 1"); - const raw = result[0]?.values?.[0]?.[0]; - return raw != null ? Number(raw) : null; + try { + const result = db.exec("SELECT gene_i_date FROM GAM_config LIMIT 1"); + const raw = result[0]?.values?.[0]?.[0]; + return raw != null ? Number(raw) : null; + } catch { + return null; + } } /**