-
Notifications
You must be signed in to change notification settings - Fork 47
feat: version-aware Connection abstraction for server scenarios #318
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
felixweinberger
wants to merge
10
commits into
main
Choose a base branch
from
fweinberger/runcontext
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+9,635
−364
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
f60e086
feat: vendor spec schema types per-version
felixweinberger 630ce5e
feat: add Connection abstraction and RunContext
felixweinberger 276cfc3
refactor: thread RunContext through ClientScenario.run
felixweinberger e7c0c09
refactor: migrate server scenarios to ctx.connect() + conn.request()
felixweinberger 649e81c
fix: normalize Connection error to JsonRpcError; clean up RunContext …
felixweinberger afd6278
fix(dns-rebinding): use version-appropriate probe body
felixweinberger 2b8b15c
feat(everything-server): route stateless carry-forward methods to Mcp…
felixweinberger 9a5dd63
refactor(connection): drop unused RequestOptions; move sdk-client; ad…
felixweinberger 9c785f0
fix: address bughunt findings (response.ok check; targetVersion naming)
felixweinberger 396c055
fix(sse-multiple-streams): keep scenario in draft; version-aware requ…
felixweinberger File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,3 +6,4 @@ dist/ | |
| .idea/ | ||
| .claude/settings.local.json | ||
| .sdk-under-test/ | ||
| .sync-schema-tmp/ | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| #!/usr/bin/env -S npx tsx | ||
| /** | ||
| * Vendor schema/{version}/schema.ts from the modelcontextprotocol spec repo | ||
| * into src/spec-types/{version}.ts at a pinned SHA. | ||
| * | ||
| * Usage: npm run sync-schema -- <sha-or-ref> | ||
| */ | ||
| import { execFileSync } from 'node:child_process'; | ||
| import { mkdirSync, writeFileSync, rmSync, copyFileSync } from 'node:fs'; | ||
| import { join } from 'node:path'; | ||
|
|
||
| const VERSIONS = ['2025-03-26', '2025-06-18', '2025-11-25', 'draft'] as const; | ||
| const SPEC_REPO = | ||
| 'https://github.com/modelcontextprotocol/modelcontextprotocol.git'; | ||
| const OUT_DIR = join(process.cwd(), 'src', 'spec-types'); | ||
|
|
||
| const ref = process.argv[2]; | ||
| if (!ref) { | ||
| console.error('Usage: npm run sync-schema -- <sha-or-ref>'); | ||
| process.exit(1); | ||
| } | ||
|
|
||
| const tmp = join(process.cwd(), '.sync-schema-tmp'); | ||
| rmSync(tmp, { recursive: true, force: true }); | ||
| mkdirSync(tmp, { recursive: true }); | ||
| mkdirSync(OUT_DIR, { recursive: true }); | ||
|
|
||
| const git = (args: string[]) => | ||
| execFileSync('git', args, { cwd: tmp, encoding: 'utf8' }); | ||
|
|
||
| try { | ||
| console.log(`Fetching ${SPEC_REPO} @ ${ref} ...`); | ||
| git(['init', '-q']); | ||
| git(['remote', 'add', 'origin', SPEC_REPO]); | ||
| git(['fetch', '-q', '--depth', '1', 'origin', ref]); | ||
| git(['checkout', '-q', 'FETCH_HEAD']); | ||
| const sha = git(['rev-parse', 'HEAD']).trim(); | ||
|
|
||
| for (const v of VERSIONS) { | ||
| copyFileSync(join(tmp, 'schema', v, 'schema.ts'), join(OUT_DIR, `${v}.ts`)); | ||
| console.log(` ${v} -> src/spec-types/${v}.ts`); | ||
| } | ||
|
|
||
| writeFileSync( | ||
| join(OUT_DIR, 'SOURCE'), | ||
| `modelcontextprotocol@${sha}\n`, | ||
| 'utf8' | ||
| ); | ||
| console.log(`Pinned: modelcontextprotocol@${sha}`); | ||
| } finally { | ||
| rmSync(tmp, { recursive: true, force: true }); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| import { describe, it, expect, vi, afterEach } from 'vitest'; | ||
| import { connectFor } from './select'; | ||
| import { connectStateful } from './stateful'; | ||
| import { connectStateless } from './stateless'; | ||
| import { JsonRpcError } from './index'; | ||
|
|
||
| describe('connectFor', () => { | ||
| it('returns stateful for dated 2025-x versions', () => { | ||
| expect(connectFor('2025-03-26')).toBe(connectStateful); | ||
| expect(connectFor('2025-06-18')).toBe(connectStateful); | ||
| expect(connectFor('2025-11-25')).toBe(connectStateful); | ||
| }); | ||
| it('returns stateless for the draft version', () => { | ||
| expect(connectFor('DRAFT-2026-v1')).toBe(connectStateless); | ||
| }); | ||
| }); | ||
|
|
||
| describe('connectStateless', () => { | ||
| const mockFetch = vi.fn(); | ||
| vi.stubGlobal('fetch', mockFetch); | ||
| afterEach(() => mockFetch.mockReset()); | ||
|
|
||
| function jsonResponse(body: unknown, status = 200) { | ||
| return new Response(JSON.stringify(body), { | ||
| status, | ||
| headers: { 'content-type': 'application/json' } | ||
| }); | ||
| } | ||
|
|
||
| function sseResponse(events: string[]) { | ||
| return new Response(events.join(''), { | ||
| status: 200, | ||
| headers: { 'content-type': 'text/event-stream' } | ||
| }); | ||
| } | ||
|
|
||
| it('injects required _meta keys and MCP-Protocol-Version header', async () => { | ||
| mockFetch.mockResolvedValue( | ||
| jsonResponse({ jsonrpc: '2.0', id: 1, result: { ok: true } }) | ||
| ); | ||
| const conn = await connectStateless('http://test/mcp'); | ||
| await conn.request('tools/list'); | ||
|
|
||
| const [, init] = mockFetch.mock.calls[0]; | ||
| expect(init.headers['MCP-Protocol-Version']).toBe('DRAFT-2026-v1'); | ||
| const sent = JSON.parse(init.body); | ||
| expect(sent.params._meta['io.modelcontextprotocol/protocolVersion']).toBe( | ||
| 'DRAFT-2026-v1' | ||
| ); | ||
| expect( | ||
| sent.params._meta['io.modelcontextprotocol/clientInfo'] | ||
| ).toBeDefined(); | ||
| expect( | ||
| sent.params._meta['io.modelcontextprotocol/clientCapabilities'] | ||
| ).toBeDefined(); | ||
| }); | ||
|
|
||
| it('throws JsonRpcError on JSON-RPC error responses', async () => { | ||
| mockFetch.mockResolvedValue( | ||
| jsonResponse({ | ||
| jsonrpc: '2.0', | ||
| id: 1, | ||
| error: { code: -32601, message: 'Method not found' } | ||
| }) | ||
| ); | ||
| const conn = await connectStateless('http://test/mcp'); | ||
| await expect(conn.request('nope')).rejects.toSatisfy( | ||
| (e) => e instanceof JsonRpcError && e.code === -32601 | ||
| ); | ||
| }); | ||
|
|
||
| it('throws on non-2xx JSON without a JSON-RPC error envelope', async () => { | ||
| mockFetch.mockResolvedValue( | ||
| jsonResponse({ detail: 'gateway rejected' }, 502) | ||
| ); | ||
| const conn = await connectStateless('http://test/mcp'); | ||
| await expect(conn.request('tools/list')).rejects.toThrow(/HTTP 502/); | ||
| }); | ||
|
|
||
| it('throws a useful error for non-JSON non-SSE responses', async () => { | ||
| mockFetch.mockResolvedValue( | ||
| new Response('<html>500</html>', { | ||
| status: 500, | ||
| headers: { 'content-type': 'text/html' } | ||
| }) | ||
| ); | ||
| const conn = await connectStateless('http://test/mcp'); | ||
| await expect(conn.request('tools/list')).rejects.toThrow(/HTTP 500/); | ||
| }); | ||
|
|
||
| it('parses SSE: collects notifications and returns final result (LF)', async () => { | ||
| mockFetch.mockResolvedValue( | ||
| sseResponse([ | ||
| 'event: message\ndata: {"jsonrpc":"2.0","method":"notifications/progress","params":{"progress":1}}\n\n', | ||
| 'event: message\ndata: {"jsonrpc":"2.0","id":1,"result":{"done":true}}\n\n' | ||
| ]) | ||
| ); | ||
| const conn = await connectStateless('http://test/mcp'); | ||
| const result = await conn.request<{ done: boolean }>('tools/call', {}); | ||
| expect(result.done).toBe(true); | ||
| expect(conn.notifications).toHaveLength(1); | ||
| expect(conn.notifications[0].method).toBe('notifications/progress'); | ||
| }); | ||
|
|
||
| it('parses SSE with CRLF line endings', async () => { | ||
| mockFetch.mockResolvedValue( | ||
| sseResponse([ | ||
| 'event: message\r\ndata: {"jsonrpc":"2.0","id":1,"result":{"ok":true}}\r\n\r\n' | ||
| ]) | ||
| ); | ||
| const conn = await connectStateless('http://test/mcp'); | ||
| const result = await conn.request<{ ok: boolean }>('tools/call', {}); | ||
| expect(result.ok).toBe(true); | ||
| }); | ||
|
|
||
| it('rejects server-to-client requests on the SSE stream', async () => { | ||
| mockFetch.mockResolvedValue( | ||
| sseResponse([ | ||
| 'event: message\ndata: {"jsonrpc":"2.0","id":99,"method":"elicitation/create","params":{}}\n\n' | ||
| ]) | ||
| ); | ||
| const conn = await connectStateless('http://test/mcp'); | ||
| await expect(conn.request('tools/call', {})).rejects.toThrow(/MRTR/); | ||
| }); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note: the other changes in everything-server here sit inside the
if (!session && (reqVersion || meta)) { ... }block which only applies in stateless mode.A stateful request falls straight through to
transport.handleRequest()unchanged.I think it would be nice to refactor everything-server to have just 2 paths though,
handleStatefulandhandleStateless, but didn't want to mix that in here.