Skip to content

Improve tool/resource type handling#368

Draft
domarmstrong wants to merge 2 commits intoSmartBear:mainfrom
domarmstrong:dom/typesafetools
Draft

Improve tool/resource type handling#368
domarmstrong wants to merge 2 commits intoSmartBear:mainfrom
domarmstrong:dom/typesafetools

Conversation

@domarmstrong
Copy link
Contributor

Goal

While looking at MCP apps, the apps need to be able to consume and call tools. Currently the tools typing (inputs/outputs) is not available to access, so you'd need to redefine all the types, or restructure the code and manually cast the inputs/outputs. This PR demonstrates a potential approach that allows accessing the tools typing by inference without requiring manual definitions.

Design

Create a new TypesafeTool class that handles the type inference (it could likely also be extended with code that currently lives in server which would make more sense being handled by the tools).

const myTool = new TypesafeTool(
   {
     title: "My Tool",
     summary: "Does something useful",
     inputSchema: z.object({ id: z.string() }),
   },
   (client: MyClient) => async (args) => {
     const result = await client.doSomething(args.id); // args has the correct type without any explicit handling
     return {
       content: [{ type: "text", text: JSON.stringify(result) }],
       structuredContent: result,
     };
   }
 );

 // Access inferred types:
 type Name = typeof myTool.name;     // "my_tool" // inferred from the title
 type Input = typeof myTool.input;   // { id: string } // inferred from the schema
 type Output = typeof myTool.output; // output can be inferred from structuredContent

This could later allow defining some hooks that could be used with mcp apps such that they are typesafe.

const { result } = useToolResult<typeof listProjects>();
await callServerTool<typeof listProjects>({
  name: 'bugsnag_list_projects',
  arguments: { apiKey: '...' },
);

Following the pattern from zephyr and reflect, use a file per tool structure which is much nicer and also gives access to the tools.

Changeset

No functional change, just refactoring

Testing

Unit tests

@tomlongridge tomlongridge requested a review from wamphlett March 17, 2026 15:40
Copy link
Contributor

@sazap10 sazap10 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change seems a good improvement, makes sense to wait for the bugsnag tool refactoring to be merged then applying this change across the board

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants