-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add pcm_search_cyclist tool #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
cc972c3
feat: add search cyclist tool functionality
mpicciolli 0536375
feat: add search cyclist tool to documentation
mpicciolli 8939169
style: improve formatting and readability of cyclist schema and searc…
mpicciolli 9cd371b
Trim inputs and rejecting calls where both are empty
mpicciolli 5246872
refactor: remove resultCount from outputSchema and adjust return stru…
mpicciolli File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,165 @@ | ||
| import type { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js"; | ||
| import { z } from "zod"; | ||
| import { withSaveDb } from "../save-db"; | ||
|
|
||
| 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)"), | ||
| 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)"), | ||
| currentAbility: z | ||
| .number() | ||
| .nullable() | ||
| .describe( | ||
| "Current ability (value_f_current_ability) — null on saves that pre-date this column", | ||
| ), | ||
| }); | ||
|
|
||
| const outputSchema = z.object({ | ||
| cyclists: z.array(cyclistSchema).describe("Matching cyclists"), | ||
| }); | ||
|
|
||
| export function registerSearchCyclist(server: McpServer): void { | ||
| server.registerTool( | ||
| "pcm_search_cyclist", | ||
| { | ||
| title: "Search PCM cyclist by name", | ||
| description: | ||
| "Search for a cyclist in a Pro Cycling Manager `.cdb` save file by first name and/or last name (case-insensitive partial match). Returns up to 10 matching cyclists with all their ratings and their country name.", | ||
| inputSchema: { | ||
| savePath: z.string().describe("Absolute path to the .cdb save file"), | ||
| firstName: z | ||
| .string() | ||
| .optional() | ||
| .describe( | ||
| "First name to search for (partial match, case-insensitive)", | ||
| ), | ||
| lastName: z | ||
| .string() | ||
| .optional() | ||
| .describe( | ||
| "Last name to search for (partial match, case-insensitive)", | ||
| ), | ||
| }, | ||
| outputSchema, | ||
| annotations: { | ||
| readOnlyHint: true, | ||
| destructiveHint: false, | ||
| idempotentHint: true, | ||
| openWorldHint: false, | ||
| }, | ||
| }, | ||
| async ({ savePath, firstName = "", lastName = "" }) => | ||
| withSaveDb(savePath, (db) => { | ||
| const columnInfo = db.exec(`PRAGMA table_info("DYN_cyclist")`); | ||
| const columnNames = new Set( | ||
| (columnInfo[0]?.values ?? []).map((r) => String(r[1])), | ||
| ); | ||
| const hasMediumMountain = columnNames.has("charac_i_medium_mountain"); | ||
| const hasCurrentAbility = columnNames.has("value_f_current_ability"); | ||
|
|
||
| const stmt = db.prepare( | ||
| `SELECT | ||
| 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, | ||
| ${hasCurrentAbility ? "c.value_f_current_ability" : "NULL"} AS currentAbility, | ||
| co.CONSTANT AS country | ||
| FROM DYN_cyclist c | ||
| LEFT JOIN STA_region r ON c.fkIDregion = r.IDregion | ||
| LEFT JOIN STA_country co ON r.fkIDcountry = co.IDcountry | ||
| WHERE LOWER(c.gene_sz_lastname) LIKE LOWER(:lastName) | ||
| AND LOWER(c.gene_sz_firstname) LIKE LOWER(:firstName) | ||
| LIMIT 10`, | ||
| ); | ||
|
|
||
| const cyclists: z.infer<typeof cyclistSchema>[] = []; | ||
| try { | ||
| const first = firstName.trim(); | ||
| const last = lastName.trim(); | ||
| if (first.length === 0 && last.length === 0) { | ||
| throw new Error("Provide at least one of firstName or lastName."); | ||
| } | ||
|
|
||
| stmt.bind({ | ||
| ":lastName": `%${last}%`, | ||
| ":firstName": `%${first}%`, | ||
| }); | ||
|
|
||
| while (stmt.step()) { | ||
| const row = stmt.getAsObject(); | ||
| cyclists.push({ | ||
| id: Number(row.IDcyclist), | ||
| 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), | ||
| currentAbility: | ||
| row.currentAbility != null ? Number(row.currentAbility) : null, | ||
| }); | ||
| } | ||
| } finally { | ||
| stmt.free(); | ||
| } | ||
|
|
||
| const output: z.infer<typeof outputSchema> = { | ||
| cyclists, | ||
| }; | ||
| return output; | ||
| }), | ||
| ); | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.