Skip to content

Commit 1c4c77a

Browse files
Add JSDoc descriptions and @example blocks to Client public methods (modelcontextprotocol#1556)
Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 3c31f96 commit 1c4c77a

2 files changed

Lines changed: 321 additions & 2 deletions

File tree

packages/client/src/client/client.examples.ts

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@
77
* @module
88
*/
99

10+
import type { Prompt, Resource, Tool } from '@modelcontextprotocol/core';
11+
1012
import { Client } from './client.js';
13+
import { SSEClientTransport } from './sse.js';
14+
import { StdioClientTransport } from './stdio.js';
15+
import { StreamableHTTPClientTransport } from './streamableHttp.js';
1116

1217
/**
1318
* Example: Using listChanged to automatically track tool and prompt updates.
@@ -36,3 +41,154 @@ function ClientOptions_listChanged() {
3641
//#endregion ClientOptions_listChanged
3742
return client;
3843
}
44+
45+
/**
46+
* Example: Connect to a local server process over stdio.
47+
*/
48+
async function Client_connect_stdio() {
49+
//#region Client_connect_stdio
50+
const client = new Client({ name: 'my-client', version: '1.0.0' });
51+
const transport = new StdioClientTransport({ command: 'my-mcp-server' });
52+
await client.connect(transport);
53+
//#endregion Client_connect_stdio
54+
return client;
55+
}
56+
57+
/**
58+
* Example: Connect with Streamable HTTP, falling back to legacy SSE.
59+
*/
60+
async function Client_connect_sseFallback(url: string) {
61+
//#region Client_connect_sseFallback
62+
const baseUrl = new URL(url);
63+
64+
try {
65+
// Try modern Streamable HTTP transport first
66+
const client = new Client({ name: 'my-client', version: '1.0.0' });
67+
const transport = new StreamableHTTPClientTransport(baseUrl);
68+
await client.connect(transport);
69+
return { client, transport };
70+
} catch {
71+
// Fall back to legacy SSE transport
72+
const client = new Client({ name: 'my-client', version: '1.0.0' });
73+
const transport = new SSEClientTransport(baseUrl);
74+
await client.connect(transport);
75+
return { client, transport };
76+
}
77+
//#endregion Client_connect_sseFallback
78+
}
79+
80+
/**
81+
* Example: Call a tool on the connected server.
82+
*/
83+
async function Client_callTool_basic(client: Client) {
84+
//#region Client_callTool_basic
85+
const result = await client.callTool({
86+
name: 'calculate-bmi',
87+
arguments: { weightKg: 70, heightM: 1.75 }
88+
});
89+
90+
// Tool-level errors are returned in the result, not thrown
91+
if (result.isError) {
92+
console.error('Tool error:', result.content);
93+
return;
94+
}
95+
96+
console.log(result.content);
97+
//#endregion Client_callTool_basic
98+
}
99+
100+
/**
101+
* Example: Access machine-readable structured output from a tool call.
102+
*/
103+
async function Client_callTool_structuredOutput(client: Client) {
104+
//#region Client_callTool_structuredOutput
105+
const result = await client.callTool({
106+
name: 'calculate-bmi',
107+
arguments: { weightKg: 70, heightM: 1.75 }
108+
});
109+
110+
// Machine-readable output for the client application
111+
if (result.structuredContent) {
112+
console.log(result.structuredContent); // e.g. { bmi: 22.86 }
113+
}
114+
//#endregion Client_callTool_structuredOutput
115+
}
116+
117+
/**
118+
* Example: Handle a sampling request from the server.
119+
*/
120+
function Client_setRequestHandler_sampling(client: Client) {
121+
//#region Client_setRequestHandler_sampling
122+
client.setRequestHandler('sampling/createMessage', async request => {
123+
const lastMessage = request.params.messages.at(-1);
124+
console.log('Sampling request:', lastMessage);
125+
126+
// In production, send messages to your LLM here
127+
return {
128+
model: 'my-model',
129+
role: 'assistant' as const,
130+
content: {
131+
type: 'text' as const,
132+
text: 'Response from the model'
133+
}
134+
};
135+
});
136+
//#endregion Client_setRequestHandler_sampling
137+
}
138+
139+
/**
140+
* Example: List tools with cursor-based pagination.
141+
*/
142+
async function Client_listTools_pagination(client: Client) {
143+
//#region Client_listTools_pagination
144+
const allTools: Tool[] = [];
145+
let cursor: string | undefined;
146+
do {
147+
const { tools, nextCursor } = await client.listTools({ cursor });
148+
allTools.push(...tools);
149+
cursor = nextCursor;
150+
} while (cursor);
151+
console.log(
152+
'Available tools:',
153+
allTools.map(t => t.name)
154+
);
155+
//#endregion Client_listTools_pagination
156+
}
157+
158+
/**
159+
* Example: List prompts with cursor-based pagination.
160+
*/
161+
async function Client_listPrompts_pagination(client: Client) {
162+
//#region Client_listPrompts_pagination
163+
const allPrompts: Prompt[] = [];
164+
let cursor: string | undefined;
165+
do {
166+
const { prompts, nextCursor } = await client.listPrompts({ cursor });
167+
allPrompts.push(...prompts);
168+
cursor = nextCursor;
169+
} while (cursor);
170+
console.log(
171+
'Available prompts:',
172+
allPrompts.map(p => p.name)
173+
);
174+
//#endregion Client_listPrompts_pagination
175+
}
176+
177+
/**
178+
* Example: List resources with cursor-based pagination.
179+
*/
180+
async function Client_listResources_pagination(client: Client) {
181+
//#region Client_listResources_pagination
182+
const allResources: Resource[] = [];
183+
let cursor: string | undefined;
184+
do {
185+
const { resources, nextCursor } = await client.listResources({ cursor });
186+
allResources.push(...resources);
187+
cursor = nextCursor;
188+
} while (cursor);
189+
console.log(
190+
'Available resources:',
191+
allResources.map(r => r.name)
192+
);
193+
//#endregion Client_listResources_pagination
194+
}

0 commit comments

Comments
 (0)