Commit graph typescript-sdk/src/server/mcp.ts
Author SHA1 Message Date
Kent C. Dodds
b293911df7 add overloads for registerResource method in McpServer class
Otherwise, TypeScript can't properly distinguish the type of callback
2025-06-18 21:56:22 -06:00
ihrpr
c90d6c9853
add context with arguments to completable 2025-06-16 14:11:10 +01:00
ihrpr
8719f93357
clean up mcp.ts 2025-06-16 11:00:20 +01:00
ihrpr
bce2dbfb83
small fixes like argsSchema 2025-06-16 10:55:02 +01:00
ihrpr
d1c94a2c69
refactor 2025-06-16 10:43:31 +01:00
ihrpr
4d03b243e3
add title 2025-06-15 20:42:25 +01:00
ihrpr
9bf5c62979
Merge pull request #619 from modelcontextprotocol/ihrpr/602
Rename ResourceReference to ResourceTemplateReference
2025-06-15 17:30:24 +01:00
ihrpr
9d3374c519
Merge pull request #574 from loopwork-ai/carlpeaslee/fix-resource-template-list-metadata
fixes metadata for listed resources from resource templates so that they favor their own metadata
2025-06-12 18:50:23 +01:00
ihrpr
255d30cbf2
Rename ResourceReference to ResourceTemplateReference 2025-06-12 15:10:12 +01:00
Carl Peaslee
6cf034595d
updates listed resources from resource templates to favor their own metadata 2025-05-30 11:32:10 -07:00
Frédéric Barthelet
5f5180c4d3 Register completions capabilities 2025-05-25 17:20:02 -07:00
Basil Hosmer
aeb7b8d741
server error if tool declares outputSchema but no structuredContent in result 2025-05-21 15:45:52 -04:00
Basil Hosmer
7636c63f39
update to track spec changes 2025-05-21 12:42:20 -04:00
Basil Hosmer
39b2f65434
restore server-side validation for tools returning structuredContent without outputSchema
Co-Authored-By: Claude <noreply@anthropic.com>
2025-05-15 10:03:50 -04:00
Basil Hosmer
712b04e96d
Refactor McpServer tool registration API
- Remove tool() overload that takes a config object with callback
- Add new registerTool() method that separates config from callback
- Extract common tool registration logic into _createRegisteredTool() helper
- Update all tests to use registerTool() for tools with output schemas
- Update examples to use registerTool() and high-level Client API
- Add comment noting that output schema validation only works with high-level API

This change improves API clarity by using a distinct method for tools with
output schemas while maintaining backward compatibility.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-05-15 10:03:50 -04:00
Basil Hosmer
6079cd8307
- add new tool() api and tests
- fix schema validation
- comment headers on tests
2025-05-15 10:03:02 -04:00
Basil Hosmer
9686fc2ead
formatting, more version rollbacks 2025-05-15 10:02:03 -04:00
Basil Hosmer
bb28098d81
feat: add server-side support for tool outputSchema with backward compatibility
- Update McpServer to support registering tools with outputSchema
- Add automatic content generation from structuredContent for backward compatibility
- Add validation to ensure proper usage of structuredContent vs content
- Add comprehensive tests for outputSchema functionality
- Add example servers demonstrating structured output usage
- Update existing test to match new backward compatibility behavior

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-05-15 10:02:02 -04:00
Basil Hosmer
a85fee407a
Add support for outputSchema and optional content fields in tools
- Add outputSchema field to Tool type and RegisteredTool interface
- Make content field optional in CallToolResult
- Update ListToolsRequestSchema handler to include outputSchema in tool list responses
- Add support for structuredContent in tool results
- Update examples to handle optional content field
- Add tests for new outputSchema and structuredContent functionality
- Update ToolCallback documentation to clarify when to use structuredContent vs content

This change enables tools to define structured output schemas and return
structured JSON content, providing better type safety and validation for
tool outputs.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-05-15 09:58:40 -04:00
Basil Hosmer
3c9496812e
tweak heuristic type test for isZodRawShape
- simpler test for empty object
- only test public zod properties (parse, safeParse)

Confirmed that vendored-library tests still pass. Note that you need the --testPathIgnorePatterns to avoid the __tests__ directory (this is what was breaking the build):
$ npm test -- --testPathIgnorePatterns="vendor"
2025-05-09 17:43:00 -04:00
Glen Maddern
b6617dc44c Fix Zod object detection logic
Previously, this check was `value instanceof ZodType`, which breaks if
the user is using a different version of Zod than the library. Instead,
do the check using the structure of the input object rather than its
prototype chain.

Additionally, this adds a vendored version of Zod to use for tests, which
ensures that regressions like this would be caught next time.
2025-05-09 15:26:33 +10:00
yuna0x0
52dc7f1160
Fix isZodRawShape return false on empty object and add test 2025-05-06 01:49:33 +08:00
Basil Hosmer
0096be432f
Add ToolAnnotations support to McpServer.tool() method 2025-04-22 15:24:32 -04:00
Justin Spahr-Summers
21f492f531
Revert "Merge pull request #334 from m-paternostro/mp/notifier"
This reverts commit 3ef902328a, reversing
changes made to 5b6d35eb12.
2025-04-22 11:35:49 +01:00
Marcelo Paternostro
112eed999c
example and more documentation 2025-04-18 10:08:36 -04:00
Marcelo Paternostro
b2fea383dd
capability event 2025-04-18 10:05:10 -04:00
Glen Maddern
9f9c244857 Added mutation methods to RegisteredTool/Resource/ResourceTemplate/Prompt
This allows these objects to be enabled/disabled, or updated on the fly. Corresponding listChanged notifications are automatically emitted.
2025-04-09 15:55:49 +10:00
ihrpr
251bb9d97d
Merge branch 'main' into ihrpr/http-streamable-server 2025-04-07 14:29:09 +01:00
Justin Spahr-Summers
8027ae1593
Revert "feat: support extending McpServer with authorization" 2025-04-07 10:47:37 +01:00
ihrpr
873f51f4ac
Merge branch 'main' into ihrpr/http-streamable-server 2025-04-06 17:39:40 +01:00
ihrpr
bafd9e74f3
Add ways to associate related requests and notifications 2025-04-05 19:52:29 +01:00
wangshijun
3571fc353f feat: support extending McpServer with subclass 2025-04-01 12:21:08 +08:00
Georges Haidar
7c7e4ad0ae
fix: drop unresolved union members in zod schemas
This change enables the strictUnions option in zod-to-json-schema so
that any union members that cannot be resolved are excluded from the
resulting JSON Schema anyOf array.

Previously, a schema like this:

**Before**

```ts
import { zodToJsonSchema } from "zod-to-json-schema";
import { z } from "zod";

console.dir(
  zodToJsonSchema(
    z.union([
      z
        .string()
        .base64()
        .transform((v) => Uint8Array.from(atob(v), (v) => v.charCodeAt(0))),
      z.instanceof(ReadableStream<Uint8Array>),
      z.instanceof(Blob),
      z.instanceof(ArrayBuffer),
      z.instanceof(Uint8Array),
    ])
  ),
  { depth: null }
);
```

Prints:

```js
{
  anyOf: [
    { type: "string", contentEncoding: "base64" }, {}, {}, {}, {}
  ],
  $schema: "http://json-schema.org/draft-07/schema#",
}
```

**After**

```ts
import { zodToJsonSchema } from "zod-to-json-schema";
import { z } from "zod";

console.dir(
  zodToJsonSchema(
    z.union([
      z
        .string()
        .base64()
        .transform((v) => Uint8Array.from(atob(v), (v) => v.charCodeAt(0))),
      z.instanceof(ReadableStream<Uint8Array>),
      z.instanceof(Blob),
      z.instanceof(ArrayBuffer),
      z.instanceof(Uint8Array),
    ]),
    { strictUnions: true }, // 👈
  ),
  { depth: null }
);
```

Prints:

```js
{
  anyOf: [
    { type: "string", contentEncoding: "base64" }
  ],
  $schema: "http://json-schema.org/draft-07/schema#",
}
```

---

I picked this example in particular because I have modelled file uploads
using a permissive schema that accepts streams, byte arrays / blobs and
base64-encoded strings. In the context of MCP, base64 would be most
applicable so I wanted to drop the other union members that couldn't be
represented in JSON Schema.
2025-02-16 19:23:29 +00:00
Justin Spahr-Summers
dbf560f24a
Don't double-add completion handler 2025-01-22 11:41:11 +00:00
Justin Spahr-Summers
2590d40e74
Fix registering multiple tools/resources/prompts
Request handlers were being re-registered every time a new tool/resource/prompt was added, which would fail since the handlers were already registered. Now we track whether handlers have been initialized and only register them once.

Added tests to verify that multiple registrations work correctly.

Resolves #124.
2025-01-21 09:14:14 +00:00
Justin Spahr-Summers
7629c70ef9
Remove unused import 2025-01-08 22:02:00 +00:00
Justin Spahr-Summers
2289dbc0d0
Factor out convenience APIs into another class 2025-01-08 21:59:38 +00:00