Skip to content
Open
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
63 changes: 63 additions & 0 deletions packages/components/nodes/tools/MCP/core.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import {
MCPToolkit,
MCPTool,
validateCommandFlags,
validateCommandInjection,
validateArgsForLocalFileAccess,
Expand All @@ -7,6 +9,67 @@ import {
} from './core'

describe('MCP Security Validations', () => {
describe('MCPTool call boundary', () => {
it('creates a per-call client with invocation headers and dispatches tools/call', async () => {
const request = jest.fn().mockResolvedValue({ content: [{ type: 'text', text: 'ok' }] })
const close = jest.fn().mockResolvedValue(undefined)
const toolkit = new MCPToolkit({ url: 'https://example.com/mcp' }, 'http')
toolkit.getToolCallHeaders = jest.fn().mockResolvedValue({ Authorization: 'Bearer per-call' })
jest.spyOn(toolkit, 'createClient').mockResolvedValue({ request, close } as any)

const mcpTool = await MCPTool({
toolkit,
name: 'lookup_customer',
description: 'Lookup a customer',
argsSchema: {
type: 'object',
properties: {
customerId: { type: 'string' }
}
}
})

const result = await mcpTool.invoke({ customerId: 'cust_123' })

expect(result).toBe(JSON.stringify([{ type: 'text', text: 'ok' }]))
expect(toolkit.getToolCallHeaders).toHaveBeenCalledTimes(1)
expect(toolkit.createClient).toHaveBeenCalledWith({ Authorization: 'Bearer per-call' })
expect(request).toHaveBeenCalledWith(
{
method: 'tools/call',
params: {
name: 'lookup_customer',
arguments: { customerId: 'cust_123' }
}
},
expect.anything()
)
expect(close).toHaveBeenCalledTimes(1)
})

it('closes the per-call client when tools/call fails', async () => {
const request = jest.fn().mockRejectedValue(new Error('tool failed'))
const close = jest.fn().mockResolvedValue(undefined)
const toolkit = new MCPToolkit({ url: 'https://example.com/mcp' }, 'http')
jest.spyOn(toolkit, 'createClient').mockResolvedValue({ request, close } as any)

const mcpTool = await MCPTool({
toolkit,
name: 'lookup_customer',
description: 'Lookup a customer',
argsSchema: {
type: 'object',
properties: {
customerId: { type: 'string' }
}
}
})

await expect(mcpTool.invoke({ customerId: 'cust_123' })).rejects.toThrow('tool failed')
expect(close).toHaveBeenCalledTimes(1)
})
})

describe('validateCommandFlags', () => {
describe('npx command', () => {
it('should block -c flag', () => {
Expand Down