@open-wa/api 5.0.0-alpha.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.
- package/.turbo/turbo-build.log +19 -0
- package/.turbo/turbo-test.log +56 -0
- package/CHANGELOG.md +25 -0
- package/LICENSE.md +67 -0
- package/README.md +19 -0
- package/dist/index.cjs +1545 -0
- package/dist/index.d.cts +151 -0
- package/dist/index.d.cts.map +1 -0
- package/package.json +46 -0
- package/src/auth/api-key.ts +28 -0
- package/src/compat/args.ts +1 -0
- package/src/compat/deprecation.ts +35 -0
- package/src/compat/session-path.ts +19 -0
- package/src/createApiMiddleware.ts +184 -0
- package/src/createApiServer.ts +414 -0
- package/src/dashboard/middleware.ts +85 -0
- package/src/docs/openapi.ts +77 -0
- package/src/docs/postman.ts +42 -0
- package/src/events/EventBridge.ts +4 -0
- package/src/events/EventBroadcaster.ts +126 -0
- package/src/execution/kernel.ts +1 -0
- package/src/health/HealthStore.ts +242 -0
- package/src/index.ts +9 -0
- package/src/invoke-client-method.ts +1 -0
- package/src/middleware/rate-limit.ts +30 -0
- package/src/monitoring/elastic.ts +104 -0
- package/src/routes/agent-discovery.ts +150 -0
- package/src/routes/debug.ts +99 -0
- package/src/routes/meta.ts +512 -0
- package/src/types.ts +31 -0
- package/test/mcp-integration.test.ts +142 -0
- package/tsconfig.json +18 -0
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import { createApiServer } from '../src/createApiServer.js';
|
|
3
|
+
import { serve } from '@hono/node-server';
|
|
4
|
+
import type { Config } from '@open-wa/config';
|
|
5
|
+
|
|
6
|
+
describe('MCP API Integration', () => {
|
|
7
|
+
it('exposes MCP capabilities in /health when disabled', async () => {
|
|
8
|
+
const config: Config = { sessionName: 'test', apiKey: 'test', cors: '*' };
|
|
9
|
+
const apiServer = createApiServer(config);
|
|
10
|
+
const app = apiServer.getApp();
|
|
11
|
+
|
|
12
|
+
const res = await app.request('/health');
|
|
13
|
+
expect(res.status).toBe(200);
|
|
14
|
+
const data = (await res.json()) as any;
|
|
15
|
+
expect(data.capabilities).toBeDefined();
|
|
16
|
+
expect(data.capabilities.mcpEnabled).toBe(false);
|
|
17
|
+
expect(data.capabilities.mcpAvailable).toBe(false);
|
|
18
|
+
expect(data.capabilities.apiKeyConfigured).toBe(true);
|
|
19
|
+
expect(data.capabilities.mcpPath).toBe('/mcp');
|
|
20
|
+
|
|
21
|
+
expect(data.host).toBeDefined();
|
|
22
|
+
expect(data.host.mcp).toBeDefined();
|
|
23
|
+
expect(data.host.mcp.enabled).toBe(false);
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it('exposes MCP capabilities in /health when enabled', async () => {
|
|
27
|
+
const config: Config = { sessionName: 'test', apiKey: 'test', cors: '*', mcp: { enabled: true } };
|
|
28
|
+
const apiServer = createApiServer(config);
|
|
29
|
+
const app = apiServer.getApp();
|
|
30
|
+
|
|
31
|
+
const res = await app.request('/health');
|
|
32
|
+
expect(res.status).toBe(200);
|
|
33
|
+
const data = (await res.json()) as any;
|
|
34
|
+
expect(data.capabilities).toBeDefined();
|
|
35
|
+
expect(data.capabilities.mcpEnabled).toBe(true);
|
|
36
|
+
// Note: mcpAvailable may be false in test if the session isn't mock "ready" depending on implementation
|
|
37
|
+
expect(data.capabilities.apiKeyConfigured).toBe(true);
|
|
38
|
+
expect(data.capabilities.mcpPath).toBe('/mcp');
|
|
39
|
+
|
|
40
|
+
expect(data.host).toBeDefined();
|
|
41
|
+
expect(data.host.mcp).toBeDefined();
|
|
42
|
+
expect(data.host.mcp.enabled).toBe(true);
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it('exposes meta mcp-tools.json', async () => {
|
|
46
|
+
const config: Config = { sessionName: 'test', apiKey: 'test', cors: '*', mcp: { enabled: true, exposeToolsMeta: true } };
|
|
47
|
+
const apiServer = createApiServer(config);
|
|
48
|
+
const app = apiServer.getApp();
|
|
49
|
+
|
|
50
|
+
const res = await app.request('/meta/mcp-tools.json');
|
|
51
|
+
expect(res.status).toBe(200);
|
|
52
|
+
const data = await res.json();
|
|
53
|
+
expect(data.endpoint).toBe('/mcp');
|
|
54
|
+
expect(data.requiresApiKey).toBe(true);
|
|
55
|
+
expect(Array.isArray(data.tools)).toBe(true);
|
|
56
|
+
expect((data.tools as any[]).length).toBeGreaterThan(0);
|
|
57
|
+
const tool = (data.tools as any[])[0];
|
|
58
|
+
expect(tool).toHaveProperty('toolName');
|
|
59
|
+
expect(tool).toHaveProperty('description');
|
|
60
|
+
expect(tool).toHaveProperty('parameterOrder');
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it('hides meta mcp-tools.json when exposeToolsMeta is false', async () => {
|
|
64
|
+
const config: Config = { sessionName: 'test', apiKey: 'test', cors: '*', mcp: { enabled: true, exposeToolsMeta: false } };
|
|
65
|
+
const apiServer = createApiServer(config);
|
|
66
|
+
const app = apiServer.getApp();
|
|
67
|
+
|
|
68
|
+
const res = await app.request('/meta/mcp-tools.json');
|
|
69
|
+
expect(res.status).toBe(404);
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it('mounts MCP on /mcp when enabled and properly secures it', async () => {
|
|
73
|
+
const config: Config = { sessionName: 'test', apiKey: 'secure_key', cors: '*', mcp: { enabled: true } };
|
|
74
|
+
const apiServer = createApiServer(config);
|
|
75
|
+
const app = apiServer.getApp();
|
|
76
|
+
|
|
77
|
+
// With no header
|
|
78
|
+
const reqNoHeader = new Request('http://localhost/mcp', {
|
|
79
|
+
method: 'POST',
|
|
80
|
+
body: JSON.stringify({
|
|
81
|
+
jsonrpc: '2.0',
|
|
82
|
+
method: 'initialize',
|
|
83
|
+
params: { protocolVersion: '2024-11-05', capabilities: {}, clientInfo: { name: 'test', version: '1.0' } },
|
|
84
|
+
id: 1,
|
|
85
|
+
}),
|
|
86
|
+
headers: {
|
|
87
|
+
'content-type': 'application/json',
|
|
88
|
+
},
|
|
89
|
+
});
|
|
90
|
+
let res = await app.request(reqNoHeader);
|
|
91
|
+
expect(res.status).toBe(401);
|
|
92
|
+
|
|
93
|
+
// With incorrect header
|
|
94
|
+
const reqBadHeader = new Request('http://localhost/mcp', {
|
|
95
|
+
method: 'POST',
|
|
96
|
+
body: JSON.stringify({
|
|
97
|
+
jsonrpc: '2.0',
|
|
98
|
+
method: 'initialize',
|
|
99
|
+
params: { protocolVersion: '2024-11-05', capabilities: {}, clientInfo: { name: 'test', version: '1.0' } },
|
|
100
|
+
id: 1,
|
|
101
|
+
}),
|
|
102
|
+
headers: {
|
|
103
|
+
'content-type': 'application/json',
|
|
104
|
+
'X-API-Key': 'wrong_key',
|
|
105
|
+
},
|
|
106
|
+
});
|
|
107
|
+
res = await app.request(reqBadHeader);
|
|
108
|
+
expect(res.status).toBe(401);
|
|
109
|
+
|
|
110
|
+
// With correct header
|
|
111
|
+
const reqHeader = new Request('http://localhost/mcp', {
|
|
112
|
+
method: 'POST',
|
|
113
|
+
body: JSON.stringify({
|
|
114
|
+
jsonrpc: '2.0',
|
|
115
|
+
method: 'initialize',
|
|
116
|
+
params: { protocolVersion: '2024-11-05', capabilities: {}, clientInfo: { name: 'test', version: '1.0' } },
|
|
117
|
+
id: 1,
|
|
118
|
+
}),
|
|
119
|
+
headers: {
|
|
120
|
+
'content-type': 'application/json',
|
|
121
|
+
'X-API-Key': 'secure_key',
|
|
122
|
+
},
|
|
123
|
+
});
|
|
124
|
+
res = await app.request(reqHeader);
|
|
125
|
+
|
|
126
|
+
// The request successfully bypassed the HTTP X-API-Key auth guard, proving auth works.
|
|
127
|
+
// However, because we hit a single-endpoint standard stream SDK mount with a raw JSON-RPC POST,
|
|
128
|
+
// the `@modelcontextprotocol/sdk` WebStandardStreamableHTTPServerTransport rejects it
|
|
129
|
+
// with 406 Not Acceptable (instead of processing it without URL params matching a session endpoint).
|
|
130
|
+
// We assert this exact SDK behavior to prove the request genuinely reached the MCP transport.
|
|
131
|
+
expect(res.status).not.toBe(401); // Auth succeeded
|
|
132
|
+
expect(res.status).not.toBe(404); // Route matched
|
|
133
|
+
expect(res.status).not.toBe(500); // No internal server error
|
|
134
|
+
expect(res.status).toBe(406); // Standard SDK rejection for invalid streaming handshake
|
|
135
|
+
|
|
136
|
+
const errData = await res.json() as any;
|
|
137
|
+
expect(errData.jsonrpc).toBe('2.0');
|
|
138
|
+
expect(errData.error).toBeDefined();
|
|
139
|
+
expect(errData.error.code).toBe(-32000);
|
|
140
|
+
expect(errData.error.message).toMatch(/Not Acceptable/);
|
|
141
|
+
});
|
|
142
|
+
});
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
{
|
|
2
|
+
"extends": "../../tsconfig.base.json",
|
|
3
|
+
"compilerOptions": {
|
|
4
|
+
"outDir": "./dist",
|
|
5
|
+
"rootDir": "./src",
|
|
6
|
+
"paths": {
|
|
7
|
+
"@open-wa/schema": [
|
|
8
|
+
"../schema/dist/index.d.ts"
|
|
9
|
+
],
|
|
10
|
+
"@open-wa/schema/*": [
|
|
11
|
+
"../schema/dist/*"
|
|
12
|
+
]
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"include": [
|
|
16
|
+
"src/**/*"
|
|
17
|
+
]
|
|
18
|
+
}
|