Implement MCP 2026-07-28 (#2286)
Co-authored-by: Matt <77928207+mattzcarey@users.noreply.github.com> Co-authored-by: Konstantin Konstantinov <KKonstantinov@users.noreply.github.com> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
This commit is contained in:
parent
e4227d135c
commit
1823aae891
1 changed files with 69131 additions and 9001 deletions
31
examples/bearer-auth/client.ts
Normal file
31
examples/bearer-auth/client.ts
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
/**
|
||||
* Asserts a bare request is `401` with a `WWW-Authenticate` header, and that
|
||||
* a request with `Authorization: Bearer demo-token` reaches the `whoami` tool
|
||||
* with the verified `authInfo`.
|
||||
*/
|
||||
import { check, parseExampleArgs } from '@mcp-examples/shared';
|
||||
import { Client, StreamableHTTPClientTransport } from '@modelcontextprotocol/client';
|
||||
|
||||
const { url, era } = parseExampleArgs();
|
||||
|
||||
// Unauthenticated → 401 + WWW-Authenticate.
|
||||
const unauth = await fetch(url, {
|
||||
method: 'POST',
|
||||
headers: { 'content-type': 'application/json', accept: 'application/json, text/event-stream' },
|
||||
body: JSON.stringify({ jsonrpc: '2.0', id: 1, method: 'ping' })
|
||||
});
|
||||
check.equal(unauth.status, 401);
|
||||
check.match(unauth.headers.get('www-authenticate') ?? '', /Bearer/);
|
||||
|
||||
// Authenticated → 200 and the tool sees the authInfo. Bearer auth is
|
||||
// HTTP-layer and era-agnostic; the client honours `--legacy` via `era`.
|
||||
const client = new Client(
|
||||
{ name: 'bearer-auth-example-client', version: '1.0.0' },
|
||||
{ versionNegotiation: { mode: era === 'modern' ? 'auto' : 'legacy' } }
|
||||
);
|
||||
await client.connect(new StreamableHTTPClientTransport(new URL(url), { authProvider: { token: async () => 'demo-token' } }));
|
||||
|
||||
const result = await client.callTool({ name: 'whoami', arguments: {} });
|
||||
check.equal(result.content?.[0]?.type === 'text' ? result.content[0].text : '', 'client=demo-client');
|
||||
|
||||
await client.close();
|
||||
Loading…
Reference in a new issue