Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/switch-mcp-server-v2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"incur": patch
---

Replaced `@modelcontextprotocol/sdk` with `@modelcontextprotocol/server` (v2), reducing dependency count by 74 packages.
3 changes: 3 additions & 0 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# TODO

- [ ] Drop `@cfworker/json-schema` dependency once `@modelcontextprotocol/server` publishes a release after `2.0.0-alpha.2` — fix was merged in [typescript-sdk#1843](https://github.com/modelcontextprotocol/typescript-sdk/pull/1843)
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@
"SKILL.md"
],
"dependencies": {
"@modelcontextprotocol/sdk": "^1.27.1",
"@cfworker/json-schema": "^4.1.1",
"@modelcontextprotocol/server": "^2.0.0-alpha.2",
"@readme/openapi-parser": "^6.0.0",
"@toon-format/toon": "^2.1.0",
"tokenx": "^1.3.0",
Expand Down
669 changes: 17 additions & 652 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

9 changes: 4 additions & 5 deletions src/Cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as fs from 'node:fs/promises'
import * as os from 'node:os'
import * as path from 'node:path'
import { estimateTokenCount, sliceByTokens } from 'tokenx'
import type { z } from 'zod'
import { z } from 'zod'

import * as Completions from './Completions.js'
import type { FieldError } from './Errors.js'
Expand Down Expand Up @@ -1539,9 +1539,8 @@ function createMcpHttpHandler(name: string, version: string) {
},
): Promise<Response> => {
if (!transport) {
const { McpServer } = await import('@modelcontextprotocol/sdk/server/mcp.js')
const { WebStandardStreamableHTTPServerTransport } =
await import('@modelcontextprotocol/sdk/server/webStandardStreamableHttp.js')
const { McpServer, WebStandardStreamableHTTPServerTransport } =
await import('@modelcontextprotocol/server')

const server = new McpServer({ name, version })

Expand All @@ -1556,7 +1555,7 @@ function createMcpHttpHandler(name: string, version: string) {
tool.name,
{
...(tool.description ? { description: tool.description } : undefined),
...(hasInput ? { inputSchema: mergedShape } : undefined),
...(hasInput ? { inputSchema: z.object(mergedShape) } : undefined),
},
async (...callArgs: any[]) => {
const params = hasInput ? (callArgs[0] as Record<string, unknown>) : {}
Expand Down
16 changes: 16 additions & 0 deletions src/Mcp.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,22 @@ describe('Mcp', () => {
expect(res.result.capabilities.tools).toBeDefined()
})

test('initialize with 2025-03-26 protocol version', async () => {
const [res] = await mcpSession(createTestCommands(), [
{
id: 1,
method: 'initialize',
params: {
protocolVersion: '2025-03-26',
capabilities: {},
clientInfo: { name: 'test-client', version: '1.0.0' },
},
},
])
expect(res.result.serverInfo).toEqual({ name: 'test-cli', version: '1.0.0' })
expect(res.result.capabilities.tools).toBeDefined()
})

test('tools/list returns all leaf commands as tools', async () => {
const [, res] = await mcpSession(createTestCommands(), [
{ id: 1, method: 'initialize', params: initParams },
Expand Down
24 changes: 15 additions & 9 deletions src/Mcp.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js'
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js'
import { McpServer, StdioServerTransport } from '@modelcontextprotocol/server'
import type { Readable, Writable } from 'node:stream'
import type { z } from 'zod'
import { z } from 'zod'

import * as Command from './internal/command.js'
import type { Handler as MiddlewareHandler } from './middleware.js'
Expand All @@ -27,7 +26,7 @@ export async function serve(
tool.name,
{
...(tool.description ? { description: tool.description } : undefined),
...(hasInput ? { inputSchema: mergedShape } : undefined),
...(hasInput ? { inputSchema: z.object(mergedShape) } : undefined),
...(tool.outputSchema ? { outputSchema: tool.outputSchema } : undefined),
} as never,
async (...callArgs: any[]) => {
Expand All @@ -36,6 +35,7 @@ export async function serve(
const extra = hasInput ? callArgs[1] : callArgs[0]
return callTool(tool, params, {
extra,
sendNotification: (n) => server.server.notification(n),
name,
version,
middlewares: options.middlewares,
Expand Down Expand Up @@ -76,9 +76,9 @@ export async function callTool(
params: Record<string, unknown>,
options: {
extra?: {
_meta?: { progressToken?: string | number }
sendNotification?: (n: any) => Promise<void>
mcpReq?: { _meta?: { progressToken?: string | number } }
}
sendNotification?: (n: ProgressNotification) => Promise<void>
name?: string | undefined
version?: string | undefined
middlewares?: MiddlewareHandler[] | undefined
Expand Down Expand Up @@ -114,13 +114,13 @@ export async function callTool(
if ('stream' in result) {
// Streaming: send progress notifications per chunk, then return buffered result
const chunks: unknown[] = []
const progressToken = options.extra?._meta?.progressToken
const progressToken = options.extra?.mcpReq?._meta?.progressToken
let i = 0
try {
for await (const chunk of result.stream) {
chunks.push(chunk)
if (progressToken !== undefined && options.extra?.sendNotification)
await options.extra.sendNotification({
if (progressToken !== undefined && options.sendNotification)
await options.sendNotification({
method: 'notifications/progress' as const,
params: { progressToken, progress: ++i, message: JSON.stringify(chunk) },
})
Expand Down Expand Up @@ -149,6 +149,12 @@ export async function callTool(
}
}

/** @internal A progress notification sent during streaming tool calls. */
type ProgressNotification = {
method: 'notifications/progress'
params: { progressToken: string | number; progress: number; message: string }
}

/** @internal A resolved tool entry from the command tree. */
export type ToolEntry = {
name: string
Expand Down
41 changes: 41 additions & 0 deletions src/e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2848,6 +2848,47 @@ describe('fetch api', () => {
`)
})

test('tools/call with no-args command', async () => {
const cli = createApp()
const sessionId = await initSession(cli)
const res = await mcpRequest(
cli,
{
jsonrpc: '2.0',
id: 6,
method: 'tools/call',
params: { name: 'ping', arguments: {} },
},
sessionId,
)
expect(res.status).toBe(200)
const body = await res.json()
expect(JSON.parse(body.result.content[0].text)).toMatchInlineSnapshot(`
{
"pong": true,
}
`)
})

test('tools/call with streaming command', async () => {
const cli = createApp()
const sessionId = await initSession(cli)
const res = await mcpRequest(
cli,
{
jsonrpc: '2.0',
id: 7,
method: 'tools/call',
params: { name: 'stream', arguments: {} },
},
sessionId,
)
expect(res.status).toBe(200)
const body = await res.json()
const chunks = JSON.parse(body.result.content[0].text)
expect(chunks).toEqual([{ content: 'hello' }, { content: 'world' }])
})

test('non-/mcp paths still work alongside MCP', async () => {
const cli = createApp()
// Initialize MCP first
Expand Down
Loading