Skip to content

Commit 3cd7447

Browse files
Merge branch 'main' into fweinberger/sep1699-debug
2 parents 74a6c66 + fab7e1e commit 3cd7447

File tree

13 files changed

+635
-1806
lines changed

13 files changed

+635
-1806
lines changed

README.md

Lines changed: 81 additions & 1792 deletions
Large diffs are not rendered by default.

docs/capabilities.md

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
## Sampling
2+
3+
MCP servers can request LLM completions from connected clients that support the sampling capability. This lets your tools offload summarisation or generation to the client’s model.
4+
5+
For a runnable server that combines tools, logging and tasks, see:
6+
7+
- [`toolWithSampleServer.ts`](../src/examples/server/toolWithSampleServer.ts)
8+
9+
In practice you will:
10+
11+
- Declare the sampling capability on the client.
12+
- Call `server.server.createMessage(...)` from within a tool handler.
13+
- Return the model’s response as structured content and/or text.
14+
15+
Refer to the MCP spec’s sampling section for full request/response details.
16+
17+
## Elicitation
18+
19+
### Form elicitation
20+
21+
Form elicitation lets a tool ask the user for additional, **non‑sensitive** information via a schema‑driven form. The server sends a schema and message, and the client is responsible for collecting and returning the data.
22+
23+
Runnable example:
24+
25+
- Server: [`elicitationFormExample.ts`](../src/examples/server/elicitationFormExample.ts)
26+
- Client‑side handling: [`simpleStreamableHttp.ts`](../src/examples/client/simpleStreamableHttp.ts)
27+
28+
The `simpleStreamableHttp` server also includes a `collect-user-info` tool that demonstrates how to drive elicitation from a tool and handle the response.
29+
30+
### URL elicitation
31+
32+
URL elicitation is designed for sensitive data and secure web‑based flows (e.g., collecting an API key, confirming a payment, or doing third‑party OAuth). Instead of returning form data, the server asks the client to open a URL and the rest of the flow happens in the browser.
33+
34+
Runnable example:
35+
36+
- Server: [`elicitationUrlExample.ts`](../src/examples/server/elicitationUrlExample.ts)
37+
- Client: [`elicitationUrlExample.ts`](../src/examples/client/elicitationUrlExample.ts)
38+
39+
Key points:
40+
41+
- Use `mode: 'url'` when calling `server.server.elicitInput(...)`.
42+
- Implement a client‑side handler for `ElicitRequestSchema` that:
43+
- Shows the full URL and reason to the user.
44+
- Asks for explicit consent.
45+
- Opens the URL in the system browser.
46+
47+
Sensitive information **must not** be collected via form elicitation; always use URL elicitation or out‑of‑band flows for secrets.
48+
49+
## Task-based execution (experimental)
50+
51+
Task-based execution enables “call-now, fetch-later” patterns for long-running operations. Instead of returning a result immediately, a tool creates a task that can be polled or resumed later.
52+
53+
The APIs live under the experimental `.experimental.tasks` namespace and may change without notice.
54+
55+
### Server-side concepts
56+
57+
On the server you will:
58+
59+
- Provide a `TaskStore` implementation that persists task metadata and results.
60+
- Enable the `tasks` capability when constructing the server.
61+
- Register tools with `server.experimental.tasks.registerToolTask(...)`.
62+
63+
For a runnable example that uses the in-memory store shipped with the SDK, see:
64+
65+
- [`toolWithSampleServer.ts`](../src/examples/server/toolWithSampleServer.ts)
66+
- `src/experimental/tasks/stores/in-memory.ts`
67+
68+
### Client-side usage
69+
70+
On the client, you use:
71+
72+
- `client.experimental.tasks.callToolStream(...)` to start a tool call that may create a task and emit status updates over time.
73+
- `client.getTask(...)` and `client.getTaskResult(...)` to check status and fetch results after reconnecting.
74+
75+
The interactive client in:
76+
77+
- [`simpleStreamableHttp.ts`](../src/examples/client/simpleStreamableHttp.ts)
78+
79+
includes commands to demonstrate calling tools that support tasks and handling their lifecycle.
80+
81+
See the MCP spec’s tasks section and the example server/client above for a full walkthrough of the task status lifecycle and TTL handling.

docs/client.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
## Client overview
2+
3+
The SDK provides a high-level `Client` class that connects to MCP servers over different transports:
4+
5+
- `StdioClientTransport` – for local processes you spawn.
6+
- `StreamableHTTPClientTransport` – for remote HTTP servers.
7+
- `SSEClientTransport` – for legacy HTTP+SSE servers (deprecated).
8+
9+
Runnable client examples live under:
10+
11+
- [`simpleStreamableHttp.ts`](../src/examples/client/simpleStreamableHttp.ts)
12+
- [`streamableHttpWithSseFallbackClient.ts`](../src/examples/client/streamableHttpWithSseFallbackClient.ts)
13+
- [`ssePollingClient.ts`](../src/examples/client/ssePollingClient.ts)
14+
- [`multipleClientsParallel.ts`](../src/examples/client/multipleClientsParallel.ts)
15+
- [`parallelToolCallsClient.ts`](../src/examples/client/parallelToolCallsClient.ts)
16+
17+
## Connecting and basic operations
18+
19+
A typical flow:
20+
21+
1. Construct a `Client` with name, version and capabilities.
22+
2. Create a transport and call `client.connect(transport)`.
23+
3. Use high-level helpers:
24+
- `listTools`, `callTool`
25+
- `listPrompts`, `getPrompt`
26+
- `listResources`, `readResource`
27+
28+
See [`simpleStreamableHttp.ts`](../src/examples/client/simpleStreamableHttp.ts) for an interactive CLI client that exercises these methods and shows how to handle notifications, elicitation and tasks.
29+
30+
## Transports and backwards compatibility
31+
32+
To support both modern Streamable HTTP and legacy SSE servers, use a client that:
33+
34+
1. Tries `StreamableHTTPClientTransport`.
35+
2. Falls back to `SSEClientTransport` on a 4xx response.
36+
37+
Runnable example:
38+
39+
- [`streamableHttpWithSseFallbackClient.ts`](../src/examples/client/streamableHttpWithSseFallbackClient.ts)
40+
41+
## OAuth client authentication helpers
42+
43+
For OAuth-secured MCP servers, the client `auth` module exposes:
44+
45+
- `ClientCredentialsProvider`
46+
- `PrivateKeyJwtProvider`
47+
- `StaticPrivateKeyJwtProvider`
48+
49+
Examples:
50+
51+
- [`simpleOAuthClient.ts`](../src/examples/client/simpleOAuthClient.ts)
52+
- [`simpleOAuthClientProvider.ts`](../src/examples/client/simpleOAuthClientProvider.ts)
53+
- [`simpleClientCredentials.ts`](../src/examples/client/simpleClientCredentials.ts)
54+
- Server-side auth demo: [`demoInMemoryOAuthProvider.ts`](../src/examples/server/demoInMemoryOAuthProvider.ts)
55+
56+
These examples show how to:
57+
58+
- Perform dynamic client registration if needed.
59+
- Acquire access tokens.
60+
- Attach OAuth credentials to Streamable HTTP requests.

docs/faq.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
## FAQ
2+
3+
<details>
4+
<summary>Table of Contents</summary>
5+
6+
- [General](#general)
7+
- [Clients](#clients)
8+
- [Servers](#servers)
9+
10+
</details>
11+
12+
## General
13+
14+
### Why do I see `TS2589: Type instantiation is excessively deep and possibly infinite` after upgrading the SDK?
15+
16+
This TypeScript error can appear when upgrading to newer SDK versions that support Zod v4 (for example, from `@modelcontextprotocol/sdk` `1.22.0` to `1.23.0`) **and** your project ends up with multiple `zod` versions in the dependency tree.
17+
18+
When there are multiple copies or versions of `zod`, TypeScript may try to instantiate very complex, cross-version types and hit its recursion limits, resulting in `TS2589`. This scenario is discussed in GitHub issue
19+
[#1180](https://github.com/modelcontextprotocol/typescript-sdk/issues/1180#event-21236550401).
20+
21+
To diagnose and fix this:
22+
23+
- **Inspect your installed `zod` versions**:
24+
- Run `npm ls zod` or `npm explain zod`, `pnpm list zod` or `pnpm why zod`, or `yarn why zod` and check whether more than one version is installed.
25+
- **Align on a single `zod` version**:
26+
- Make sure all packages that depend on `zod` use a compatible version range so that your package manager can hoist a single copy.
27+
- In monorepos, consider declaring `zod` at the workspace root and using compatible ranges in individual packages.
28+
- **Use overrides/resolutions if necessary**:
29+
- With npm, Yarn, or pnpm, you can use `overrides` / `resolutions` to force a single `zod` version if some transitive dependencies pull in a different one.
30+
31+
Once your project is using a single, compatible `zod` version, the `TS2589` error should no longer occur.
32+
33+
## Clients
34+
35+
### How do I enable Web Crypto (`globalThis.crypto`) for client authentication in older Node.js versions?
36+
37+
The SDK’s OAuth client authentication helpers (for example, those in `src/client/auth-extensions.ts` that use `jose`) rely on the Web Crypto API exposed as `globalThis.crypto`. This is especially important for **client credentials** and **JWT-based** authentication flows used by
38+
MCP clients.
39+
40+
- **Node.js v19.0.0 and later**: `globalThis.crypto` is available by default.
41+
- **Node.js v18.x**: `globalThis.crypto` may not be defined by default. In this repository we polyfill it for tests (see `vitest.setup.ts`), and you should do the same in your app if it is missing – or alternatively, run Node with `--experimental-global-webcrypto` as per your
42+
Node version documentation. (See https://nodejs.org/dist/latest-v18.x/docs/api/globals.html#crypto )
43+
44+
If you run clients on Node.js versions where `globalThis.crypto` is missing, you can polyfill it using the built-in `node:crypto` module, similar to the SDK's own `vitest.setup.ts`:
45+
46+
```typescript
47+
import { webcrypto } from 'node:crypto';
48+
49+
if (typeof globalThis.crypto === 'undefined') {
50+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
51+
(globalThis as any).crypto = webcrypto as unknown as Crypto;
52+
}
53+
```
54+
55+
For production use, you can either:
56+
57+
- Run clients on a Node.js version where `globalThis.crypto` is available by default (recommended), or
58+
- Apply a similar polyfill early in your client's startup code when targeting older Node.js runtimes, so that OAuth client authentication works reliably.
59+
60+
## Servers
61+
62+
### Where can I find runnable server examples?
63+
64+
The SDK ships several runnable server examples under `src/examples/server`. The root `README.md` contains a curated **Server examples** table that links to each scenario (stateful/stateless Streamable HTTP, JSON-only mode, SSE/backwards compatibility, elicitation, sampling,
65+
tasks, and OAuth demos), and `src/examples/README.md` includes commands and deployment diagrams for running them.

0 commit comments

Comments
 (0)