@open-wa/mcp 5.0.0-alpha.8 → 5.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,181 +0,0 @@
1
- /**
2
- * Tests for MCP adapter: auth enforcement, readiness gating, and transport shape.
3
- *
4
- * These tests verify the adapter at the Hono layer by creating a real
5
- * Hono app, mounting the adapter, and making requests via app.request().
6
- */
7
- import { describe, it, expect, vi, beforeAll } from 'vitest';
8
- import { Hono } from 'hono';
9
- import { createHonoMcpAdapter } from '../src/adapter/hono-mcp';
10
- import type { Config } from '@open-wa/schema';
11
-
12
- function createTestConfig(overrides: Partial<Config> = {}): Config {
13
- return {
14
- sessionId: 'test-session',
15
- apiKey: 'test-api-key-123',
16
- mcp: { enabled: true, path: '/mcp', exposeToolsMeta: true },
17
- ...overrides,
18
- } as Config;
19
- }
20
-
21
- describe('createHonoMcpAdapter', () => {
22
- describe('API-key auth enforcement', () => {
23
- it('should reject requests without API key', async () => {
24
- const config = createTestConfig();
25
- const app = new Hono();
26
- const adapter = createHonoMcpAdapter({
27
- config,
28
- clientSource: () => ({}),
29
- });
30
- adapter.mount(app, '/mcp');
31
-
32
- const res = await app.request('/mcp', {
33
- method: 'POST',
34
- headers: { 'Content-Type': 'application/json' },
35
- body: JSON.stringify({ jsonrpc: '2.0', method: 'initialize', id: 1 }),
36
- });
37
-
38
- expect(res.status).toBe(401);
39
- const body = await res.json();
40
- expect(body.error).toBe('Unauthorized');
41
- });
42
-
43
- it('should reject requests with wrong API key', async () => {
44
- const config = createTestConfig();
45
- const app = new Hono();
46
- const adapter = createHonoMcpAdapter({
47
- config,
48
- clientSource: () => ({}),
49
- });
50
- adapter.mount(app, '/mcp');
51
-
52
- const res = await app.request('/mcp', {
53
- method: 'POST',
54
- headers: {
55
- 'Content-Type': 'application/json',
56
- 'X-API-Key': 'wrong-key',
57
- },
58
- body: JSON.stringify({ jsonrpc: '2.0', method: 'initialize', id: 1 }),
59
- });
60
-
61
- expect(res.status).toBe(401);
62
- });
63
-
64
- it('should accept requests with correct API key via header', async () => {
65
- const config = createTestConfig();
66
- const app = new Hono();
67
- const adapter = createHonoMcpAdapter({
68
- config,
69
- clientSource: () => ({}),
70
- });
71
- adapter.mount(app, '/mcp');
72
-
73
- const res = await app.request('/mcp', {
74
- method: 'POST',
75
- headers: {
76
- 'Content-Type': 'application/json',
77
- 'X-API-Key': 'test-api-key-123',
78
- },
79
- body: JSON.stringify({
80
- jsonrpc: '2.0',
81
- method: 'initialize',
82
- id: 1,
83
- params: {
84
- protocolVersion: '2025-03-26',
85
- capabilities: {},
86
- clientInfo: { name: 'test', version: '1.0' },
87
- },
88
- }),
89
- });
90
-
91
- // Should not be 401 — transport takes over
92
- expect(res.status).not.toBe(401);
93
- });
94
-
95
- });
96
-
97
- describe('Streamable HTTP transport shape', () => {
98
- it('should handle POST requests (JSON-RPC messages)', async () => {
99
- const config = createTestConfig();
100
- const app = new Hono();
101
- const adapter = createHonoMcpAdapter({
102
- config,
103
- clientSource: () => ({}),
104
- });
105
- adapter.mount(app, '/mcp');
106
-
107
- const res = await app.request('/mcp', {
108
- method: 'POST',
109
- headers: {
110
- 'Content-Type': 'application/json',
111
- 'X-API-Key': 'test-api-key-123',
112
- },
113
- body: JSON.stringify({
114
- jsonrpc: '2.0',
115
- method: 'initialize',
116
- id: 1,
117
- params: {
118
- protocolVersion: '2025-03-26',
119
- capabilities: {},
120
- clientInfo: { name: 'test', version: '1.0' },
121
- },
122
- }),
123
- });
124
-
125
- // MCP initialization should respond (not a 404 or 405)
126
- expect(res.status).not.toBe(404);
127
- expect(res.status).not.toBe(405);
128
- });
129
-
130
- it('should handle DELETE requests (session termination)', async () => {
131
- const config = createTestConfig();
132
- const app = new Hono();
133
- const adapter = createHonoMcpAdapter({
134
- config,
135
- clientSource: () => ({}),
136
- });
137
- adapter.mount(app, '/mcp');
138
-
139
- const res = await app.request('/mcp', {
140
- method: 'DELETE',
141
- headers: {
142
- 'X-API-Key': 'test-api-key-123',
143
- },
144
- });
145
-
146
- // Should be handled by transport, not 404
147
- expect(res.status).not.toBe(404);
148
- });
149
- });
150
-
151
- describe('lifecycle/readiness gating', () => {
152
- it('should block tool calls when session is not connected', async () => {
153
- const config = createTestConfig();
154
- const app = new Hono();
155
- const adapter = createHonoMcpAdapter({
156
- config,
157
- clientSource: () => ({}),
158
- isSessionConnected: () => false,
159
- });
160
- adapter.mount(app, '/mcp');
161
-
162
- // Note: We can't easily test tools/call through the full MCP protocol
163
- // in a unit test because it requires session initialization first.
164
- // The readiness check is verified structurally in the adapter code.
165
- // Integration tests would cover the full flow.
166
- expect(adapter).toBeDefined();
167
- });
168
-
169
- it('should allow tool calls when apiLifecycle is immediate regardless of readiness', () => {
170
- const config = createTestConfig({ apiLifecycle: 'immediate' } as any);
171
- const adapter = createHonoMcpAdapter({
172
- config,
173
- clientSource: () => ({}),
174
- isSessionConnected: () => false,
175
- });
176
-
177
- // Adapter should be created successfully with immediate lifecycle
178
- expect(adapter).toBeDefined();
179
- });
180
- });
181
- });
@@ -1,151 +0,0 @@
1
- /**
2
- * Tests for the shared execution kernel.
3
- *
4
- * These tests verify that executeCapability produces identical
5
- * normalization, validation, and error shaping as the HTTP path.
6
- */
7
- import { describe, it, expect, vi } from 'vitest';
8
- import { executeCapability, type ExecutionContext } from '../src/execution/kernel';
9
- import { z } from 'zod';
10
- import type { HttpMethodDefinition } from '@open-wa/schema';
11
-
12
- /**
13
- * Helper to create a minimal HttpMethodDefinition-like object for testing.
14
- */
15
- function createMockDefinition(overrides: Partial<HttpMethodDefinition> = {}): HttpMethodDefinition {
16
- return {
17
- functionName: 'sendText',
18
- namespacedName: 'sendText',
19
- path: '/api/messages/sendText',
20
- namespace: 'messages',
21
- description: 'Send a text message',
22
- httpMethod: 'POST',
23
- inputSchema: z.object({
24
- to: z.string(),
25
- content: z.string(),
26
- }),
27
- outputSchema: z.any(),
28
- parameterOrder: ['to', 'content'],
29
- aliases: [],
30
- deprecatedAliases: [],
31
- aliasRoutes: [],
32
- routeSignatures: [],
33
- invocationNames: ['sendText'],
34
- keyAliasMap: {},
35
- ...overrides,
36
- };
37
- }
38
-
39
- describe('executeCapability', () => {
40
- it('should return success with data when invocation succeeds', async () => {
41
- const mockClient = {
42
- sendText: vi.fn().mockResolvedValue({ id: 'msg-123' }),
43
- };
44
-
45
- const result = await executeCapability({
46
- client: mockClient,
47
- definition: createMockDefinition(),
48
- payload: { to: '1234567890@c.us', content: 'Hello' },
49
- sessionId: 'test-session',
50
- });
51
-
52
- expect(result.success).toBe(true);
53
- expect(result.status).toBe(200);
54
- expect(result.data).toEqual({ id: 'msg-123' });
55
- expect(mockClient.sendText).toHaveBeenCalled();
56
- });
57
-
58
- it('should return 400 for Zod validation errors', async () => {
59
- const mockClient = {
60
- sendText: vi.fn(),
61
- };
62
-
63
- const result = await executeCapability({
64
- client: mockClient,
65
- definition: createMockDefinition(),
66
- payload: { to: 123, content: null } as any, // invalid types
67
- sessionId: 'test-session',
68
- });
69
-
70
- expect(result.success).toBe(false);
71
- expect(result.status).toBe(400);
72
- expect(result.error).toBe('Validation Error');
73
- expect(result.details).toBeDefined();
74
- expect(mockClient.sendText).not.toHaveBeenCalled();
75
- });
76
-
77
- it('should return 500 for client runtime errors', async () => {
78
- const mockClient = {
79
- sendText: vi.fn().mockRejectedValue(new Error('WhatsApp connection lost')),
80
- };
81
-
82
- const result = await executeCapability({
83
- client: mockClient,
84
- definition: createMockDefinition(),
85
- payload: { to: '1234567890@c.us', content: 'Hello' },
86
- sessionId: 'test-session',
87
- });
88
-
89
- expect(result.success).toBe(false);
90
- expect(result.status).toBe(500);
91
- expect(result.error).toBe('WhatsApp connection lost');
92
- });
93
-
94
- it('should log success to elasticEmitter when provided', async () => {
95
- const mockEmitter = { log: vi.fn() };
96
- const mockClient = {
97
- sendText: vi.fn().mockResolvedValue('ok'),
98
- };
99
-
100
- await executeCapability({
101
- client: mockClient,
102
- definition: createMockDefinition(),
103
- payload: { to: '1234567890@c.us', content: 'Hello' },
104
- sessionId: 'test-session',
105
- elasticEmitter: mockEmitter,
106
- });
107
-
108
- expect(mockEmitter.log).toHaveBeenCalledWith(
109
- expect.objectContaining({
110
- level: 'info',
111
- method: 'sendText',
112
- statusCode: 200,
113
- }),
114
- );
115
- });
116
-
117
- it('should log errors to elasticEmitter when provided', async () => {
118
- const mockEmitter = { log: vi.fn() };
119
- const mockClient = {
120
- sendText: vi.fn().mockRejectedValue(new Error('fail')),
121
- };
122
-
123
- await executeCapability({
124
- client: mockClient,
125
- definition: createMockDefinition(),
126
- payload: { to: '1234567890@c.us', content: 'Hello' },
127
- sessionId: 'test-session',
128
- elasticEmitter: mockEmitter,
129
- });
130
-
131
- expect(mockEmitter.log).toHaveBeenCalledWith(
132
- expect.objectContaining({
133
- level: 'error',
134
- method: 'sendText',
135
- statusCode: 500,
136
- }),
137
- );
138
- });
139
-
140
- it('should handle undefined client gracefully', async () => {
141
- const result = await executeCapability({
142
- client: undefined,
143
- definition: createMockDefinition(),
144
- payload: { to: '1234567890@c.us', content: 'Hello' },
145
- sessionId: 'test-session',
146
- });
147
-
148
- expect(result.success).toBe(false);
149
- expect(result.status).toBe(500);
150
- });
151
- });
@@ -1,61 +0,0 @@
1
- /**
2
- * Tests for MCP manifest generation from schema registry.
3
- */
4
- import { describe, it, expect } from 'vitest';
5
- import { getMcpToolDefinitions, type McpToolDefinition } from '../src/manifest';
6
- import { getCanonicalToolName } from '../src/tool-naming';
7
-
8
- describe('getMcpToolDefinitions', () => {
9
- let tools: McpToolDefinition[];
10
-
11
- // We import @open-wa/schema/methods to populate the registry
12
- beforeAll(async () => {
13
- await import('@open-wa/schema/methods');
14
- tools = getMcpToolDefinitions();
15
- });
16
-
17
- it('should return a non-empty array of tool definitions', () => {
18
- expect(tools).toBeInstanceOf(Array);
19
- expect(tools.length).toBeGreaterThan(0);
20
- });
21
-
22
- it('should produce one tool per method (no duplicates)', () => {
23
- const names = tools.map((t) => t.toolName);
24
- const unique = new Set(names);
25
- expect(unique.size).toBe(names.length);
26
- });
27
-
28
- it('every tool should have a namespace.method toolName shape', () => {
29
- for (const tool of tools) {
30
- expect(tool.toolName).toMatch(/^[a-zA-Z]+\.[a-zA-Z]+/);
31
- expect(tool.toolName).toBe(getCanonicalToolName(tool.namespace, tool.namespacedName));
32
- }
33
- });
34
-
35
- it('every tool should have a valid Zod inputSchema', () => {
36
- for (const tool of tools) {
37
- expect(tool.inputSchema).toBeDefined();
38
- expect(typeof tool.inputSchema.parse).toBe('function');
39
- }
40
- });
41
-
42
- it('every tool should carry parameterOrder and keyAliasMap', () => {
43
- for (const tool of tools) {
44
- expect(tool.parameterOrder).toBeInstanceOf(Array);
45
- expect(tool.keyAliasMap).toBeDefined();
46
- expect(typeof tool.keyAliasMap).toBe('object');
47
- }
48
- });
49
-
50
- it('should include annotations with readOnlyHint for getters', () => {
51
- const getters = tools.filter((t) => t.functionName.startsWith('get') || t.functionName.startsWith('is'));
52
- for (const getter of getters) {
53
- expect(getter.annotations?.readOnlyHint).toBe(true);
54
- }
55
-
56
- const mutators = tools.filter((t) => t.functionName.startsWith('send'));
57
- for (const mutator of mutators) {
58
- expect(mutator.annotations?.readOnlyHint).toBeFalsy();
59
- }
60
- });
61
- });
package/tsdown.config.ts DELETED
@@ -1,12 +0,0 @@
1
- import { defineConfig } from 'tsdown';
2
-
3
- export default defineConfig({
4
- entry: {
5
- index: './src/index.ts',
6
- },
7
- format: ['esm', 'cjs'],
8
- dts: true,
9
- sourcemap: true,
10
- clean: true,
11
- treeshake: true,
12
- });
package/vitest.config.ts DELETED
@@ -1,10 +0,0 @@
1
- import { defineConfig } from 'vitest/config';
2
-
3
- export default defineConfig({
4
- test: {
5
- globals: true,
6
- environment: 'node',
7
- include: ['test/**/*.test.ts'],
8
- testTimeout: 30000,
9
- },
10
- });