Skip to content
Draft
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/mcp-client-inject.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'mppx': patch
---

Added MCP SDK client helpers for automatic MPP payments and Tempo Accounts Provider-backed clients.
4 changes: 4 additions & 0 deletions src/Mcp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ export const paymentVerificationFailedCode = -32043
/** MCP metadata key for credentials. */
export const credentialMetaKey = 'org.paymentauth/credential'

/** MCP metadata key for payment-required tool results. */
export const paymentRequiredMetaKey = 'org.paymentauth/payment-required'

/** MCP metadata key for receipts. */
export const receiptMetaKey = 'org.paymentauth/receipt'

Expand Down Expand Up @@ -45,6 +48,7 @@ export type JsonRpcRequest = Request & {
*/
export type Result = {
_meta?: {
[paymentRequiredMetaKey]?: ErrorObject['data']
[receiptMetaKey]?: Receipt
[key: string]: unknown
}
Expand Down
122 changes: 122 additions & 0 deletions src/mcp-sdk/client/McpClient.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { Client } from '@modelcontextprotocol/sdk/client/index.js'
import { tempo } from 'mppx/client'
import type { Account } from 'viem'
import type { JsonRpcAccount } from 'viem/accounts'

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can this file just be src/mcp?

you should be able to import via 'mppx/mcp/client'

import { describe, expectTypeOf, test } from 'vp/test'

import * as McpClient from './McpClient.js'
Expand Down Expand Up @@ -108,3 +109,124 @@ describe('McpClient.wrap.CallToolOptions', () => {
expectTypeOf<Options>().toHaveProperty('timeout')
})
})

describe('McpClient.inject', () => {
test('returns the original client with payment-aware callTool', () => {
const client = {} as Client
const injected = McpClient.inject(client, {
methods: [
tempo({
account: {} as Account,
}),
],
})

expectTypeOf(injected).toEqualTypeOf<
McpClient.inject.McpClient<Client, readonly [ReturnType<typeof tempo>]>
>()
expectTypeOf(injected.callTool).toBeFunction()
expectTypeOf(injected.callTool).returns.toExtend<Promise<McpClient.CallToolResult>>()
})

test('callTool keeps the MCP SDK result schema and options positions', () => {
const client = {} as Client
const injected = McpClient.inject(client, {
methods: [
tempo({
account: {} as Account,
}),
],
})

expectTypeOf(injected.callTool).toBeCallableWith({ name: 'tool' }, undefined, {
timeout: 5000,
})
})

test('callTool accepts context when method has context', () => {
const client = {} as Client
const injected = McpClient.inject(client, {
methods: [tempo({})],
})

expectTypeOf(injected.callTool).toBeCallableWith(
{ name: 'tool' },
{ context: { account: {} as Account } },
)
})
})

describe('McpClient.inject.McpClient', () => {
test('has callTool with correct signature', () => {
type InjectedClient = McpClient.inject.McpClient

expectTypeOf<InjectedClient>().toHaveProperty('callTool')
})
})

describe('McpClient.inject.CallToolOptions', () => {
test('has context and timeout properties', () => {
type Options = McpClient.inject.CallToolOptions

expectTypeOf<Options>().toHaveProperty('context')
expectTypeOf<Options>().toHaveProperty('timeout')
})
})

describe('withMppClient', () => {
test('returns the original client as an injected client', () => {
const client = {} as Client
const injected = McpClient.withMppClient(client, {
methods: [
tempo({
account: {} as Account,
}),
],
})

expectTypeOf(injected).toEqualTypeOf<
McpClient.withMppClient.McpClient<Client, readonly [ReturnType<typeof tempo>]>
>()
expectTypeOf(injected.callTool).returns.toExtend<Promise<McpClient.CallToolResult>>()
})

test('accepts Tempo Accounts Provider-backed methods', () => {
const client = {} as Client
const provider = {
getAccount: () => ({
address: '0x0000000000000000000000000000000000000001',
type: 'json-rpc',
}),
getClient: () => ({}) as import('viem').Client,
} as {
getAccount(): JsonRpcAccount
getClient(): import('viem').Client
}

const injected = McpClient.withMppClient(client, {
methods: [tempo({ provider })],
})

expectTypeOf(injected.callTool).returns.toExtend<Promise<McpClient.CallToolResult>>()
})

test('can store an inferred client as the exported client type', () => {
const client = {} as Client
const provider = {
getAccount: () => ({
address: '0x0000000000000000000000000000000000000001',
type: 'json-rpc',
}),
getClient: () => ({}) as import('viem').Client,
} as {
getAccount(): JsonRpcAccount
getClient(): import('viem').Client
}

const injected = McpClient.withMppClient(client, {
methods: [tempo({ provider })],
})

expectTypeOf(injected).toMatchTypeOf<McpClient.withMppClient.McpClient>()
})
})
Loading