@coffeerunhobby/mcp-ha-connect 0.5.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/.dockerignore +8 -0
- package/.env.example +149 -0
- package/.gitattributes +30 -0
- package/Dockerfile +33 -0
- package/LICENSE +21 -0
- package/README.md +394 -0
- package/dist/config.d.ts +33 -0
- package/dist/config.d.ts.map +1 -0
- package/dist/config.js +183 -0
- package/dist/config.js.map +1 -0
- package/dist/haClient/automations.d.ts +67 -0
- package/dist/haClient/automations.d.ts.map +1 -0
- package/dist/haClient/automations.js +181 -0
- package/dist/haClient/automations.js.map +1 -0
- package/dist/haClient/config.d.ts +28 -0
- package/dist/haClient/config.d.ts.map +1 -0
- package/dist/haClient/config.js +38 -0
- package/dist/haClient/config.js.map +1 -0
- package/dist/haClient/devices.d.ts +219 -0
- package/dist/haClient/devices.d.ts.map +1 -0
- package/dist/haClient/devices.js +506 -0
- package/dist/haClient/devices.js.map +1 -0
- package/dist/haClient/entities.d.ts +35 -0
- package/dist/haClient/entities.d.ts.map +1 -0
- package/dist/haClient/entities.js +97 -0
- package/dist/haClient/entities.js.map +1 -0
- package/dist/haClient/events.d.ts +138 -0
- package/dist/haClient/events.d.ts.map +1 -0
- package/dist/haClient/events.js +344 -0
- package/dist/haClient/events.js.map +1 -0
- package/dist/haClient/history.d.ts +26 -0
- package/dist/haClient/history.d.ts.map +1 -0
- package/dist/haClient/history.js +54 -0
- package/dist/haClient/history.js.map +1 -0
- package/dist/haClient/index.d.ts +153 -0
- package/dist/haClient/index.d.ts.map +1 -0
- package/dist/haClient/index.js +223 -0
- package/dist/haClient/index.js.map +1 -0
- package/dist/haClient/request.d.ts +34 -0
- package/dist/haClient/request.d.ts.map +1 -0
- package/dist/haClient/request.js +93 -0
- package/dist/haClient/request.js.map +1 -0
- package/dist/haClient/services.d.ts +22 -0
- package/dist/haClient/services.d.ts.map +1 -0
- package/dist/haClient/services.js +39 -0
- package/dist/haClient/services.js.map +1 -0
- package/dist/haClient/states.d.ts +26 -0
- package/dist/haClient/states.d.ts.map +1 -0
- package/dist/haClient/states.js +53 -0
- package/dist/haClient/states.js.map +1 -0
- package/dist/haClient/updates.d.ts +22 -0
- package/dist/haClient/updates.d.ts.map +1 -0
- package/dist/haClient/updates.js +48 -0
- package/dist/haClient/updates.js.map +1 -0
- package/dist/haClient.d.ts +97 -0
- package/dist/haClient.d.ts.map +1 -0
- package/dist/haClient.js +344 -0
- package/dist/haClient.js.map +1 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +92 -0
- package/dist/index.js.map +1 -0
- package/dist/localAI/index.d.ts +37 -0
- package/dist/localAI/index.d.ts.map +1 -0
- package/dist/localAI/index.js +73 -0
- package/dist/localAI/index.js.map +1 -0
- package/dist/localAI/prompts.d.ts +20 -0
- package/dist/localAI/prompts.d.ts.map +1 -0
- package/dist/localAI/prompts.js +48 -0
- package/dist/localAI/prompts.js.map +1 -0
- package/dist/localAI/providers/base.d.ts +51 -0
- package/dist/localAI/providers/base.d.ts.map +1 -0
- package/dist/localAI/providers/base.js +80 -0
- package/dist/localAI/providers/base.js.map +1 -0
- package/dist/localAI/providers/ollama.d.ts +22 -0
- package/dist/localAI/providers/ollama.d.ts.map +1 -0
- package/dist/localAI/providers/ollama.js +63 -0
- package/dist/localAI/providers/ollama.js.map +1 -0
- package/dist/localAI/providers/openai.d.ts +23 -0
- package/dist/localAI/providers/openai.d.ts.map +1 -0
- package/dist/localAI/providers/openai.js +63 -0
- package/dist/localAI/providers/openai.js.map +1 -0
- package/dist/localAI/types.d.ts +61 -0
- package/dist/localAI/types.d.ts.map +1 -0
- package/dist/localAI/types.js +6 -0
- package/dist/localAI/types.js.map +1 -0
- package/dist/ollamaClient.d.ts +37 -0
- package/dist/ollamaClient.d.ts.map +1 -0
- package/dist/ollamaClient.js +110 -0
- package/dist/ollamaClient.js.map +1 -0
- package/dist/resources/index.d.ts +11 -0
- package/dist/resources/index.d.ts.map +1 -0
- package/dist/resources/index.js +238 -0
- package/dist/resources/index.js.map +1 -0
- package/dist/server/common.d.ts +9 -0
- package/dist/server/common.d.ts.map +1 -0
- package/dist/server/common.js +27 -0
- package/dist/server/common.js.map +1 -0
- package/dist/server/eventSubscription.d.ts +53 -0
- package/dist/server/eventSubscription.d.ts.map +1 -0
- package/dist/server/eventSubscription.js +257 -0
- package/dist/server/eventSubscription.js.map +1 -0
- package/dist/server/http.d.ts +12 -0
- package/dist/server/http.d.ts.map +1 -0
- package/dist/server/http.js +491 -0
- package/dist/server/http.js.map +1 -0
- package/dist/server/rateLimiter.d.ts +70 -0
- package/dist/server/rateLimiter.d.ts.map +1 -0
- package/dist/server/rateLimiter.js +156 -0
- package/dist/server/rateLimiter.js.map +1 -0
- package/dist/server/sse.d.ts +32 -0
- package/dist/server/sse.d.ts.map +1 -0
- package/dist/server/sse.js +76 -0
- package/dist/server/sse.js.map +1 -0
- package/dist/server/stdio.d.ts +8 -0
- package/dist/server/stdio.d.ts.map +1 -0
- package/dist/server/stdio.js +16 -0
- package/dist/server/stdio.js.map +1 -0
- package/dist/server/stream.d.ts +26 -0
- package/dist/server/stream.d.ts.map +1 -0
- package/dist/server/stream.js +81 -0
- package/dist/server/stream.js.map +1 -0
- package/dist/server.d.ts +8 -0
- package/dist/server.d.ts.map +1 -0
- package/dist/server.js +30 -0
- package/dist/server.js.map +1 -0
- package/dist/tools/callService.d.ts +8 -0
- package/dist/tools/callService.d.ts.map +1 -0
- package/dist/tools/callService.js +100 -0
- package/dist/tools/callService.js.map +1 -0
- package/dist/tools/getClient.d.ts +8 -0
- package/dist/tools/getClient.d.ts.map +1 -0
- package/dist/tools/getClient.js +94 -0
- package/dist/tools/getClient.js.map +1 -0
- package/dist/tools/getDevice.d.ts +8 -0
- package/dist/tools/getDevice.d.ts.map +1 -0
- package/dist/tools/getDevice.js +94 -0
- package/dist/tools/getDevice.js.map +1 -0
- package/dist/tools/getEntitiesByDomain.d.ts +8 -0
- package/dist/tools/getEntitiesByDomain.d.ts.map +1 -0
- package/dist/tools/getEntitiesByDomain.js +80 -0
- package/dist/tools/getEntitiesByDomain.js.map +1 -0
- package/dist/tools/getState.d.ts +8 -0
- package/dist/tools/getState.d.ts.map +1 -0
- package/dist/tools/getState.js +90 -0
- package/dist/tools/getState.js.map +1 -0
- package/dist/tools/getStates.d.ts +8 -0
- package/dist/tools/getStates.d.ts.map +1 -0
- package/dist/tools/getStates.js +70 -0
- package/dist/tools/getStates.js.map +1 -0
- package/dist/tools/index.d.ts +9 -0
- package/dist/tools/index.d.ts.map +1 -0
- package/dist/tools/index.js +9 -0
- package/dist/tools/index.js.map +1 -0
- package/dist/tools/listClients.d.ts +8 -0
- package/dist/tools/listClients.d.ts.map +1 -0
- package/dist/tools/listClients.js +73 -0
- package/dist/tools/listClients.js.map +1 -0
- package/dist/tools/listDevices.d.ts +8 -0
- package/dist/tools/listDevices.d.ts.map +1 -0
- package/dist/tools/listDevices.js +73 -0
- package/dist/tools/listDevices.js.map +1 -0
- package/dist/tools/listSites.d.ts +8 -0
- package/dist/tools/listSites.d.ts.map +1 -0
- package/dist/tools/listSites.js +67 -0
- package/dist/tools/listSites.js.map +1 -0
- package/dist/tools/registry.d.ts +9 -0
- package/dist/tools/registry.d.ts.map +1 -0
- package/dist/tools/registry.js +1382 -0
- package/dist/tools/registry.js.map +1 -0
- package/dist/tools/searchDevices.d.ts +8 -0
- package/dist/tools/searchDevices.d.ts.map +1 -0
- package/dist/tools/searchDevices.js +84 -0
- package/dist/tools/searchDevices.js.map +1 -0
- package/dist/tools/searchEntities.d.ts +8 -0
- package/dist/tools/searchEntities.d.ts.map +1 -0
- package/dist/tools/searchEntities.js +80 -0
- package/dist/tools/searchEntities.js.map +1 -0
- package/dist/types/entity.d.ts +98 -0
- package/dist/types/entity.d.ts.map +1 -0
- package/dist/types/entity.js +2 -0
- package/dist/types/entity.js.map +1 -0
- package/dist/types/errors.d.ts +16 -0
- package/dist/types/errors.d.ts.map +1 -0
- package/dist/types/errors.js +32 -0
- package/dist/types/errors.js.map +1 -0
- package/dist/types/haConfig.d.ts +8 -0
- package/dist/types/haConfig.d.ts.map +1 -0
- package/dist/types/haConfig.js +2 -0
- package/dist/types/haConfig.js.map +1 -0
- package/dist/types/index.d.ts +6 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +2 -0
- package/dist/types/index.js.map +1 -0
- package/dist/types/request.d.ts +7 -0
- package/dist/types/request.d.ts.map +1 -0
- package/dist/types/request.js +2 -0
- package/dist/types/request.js.map +1 -0
- package/dist/types/serviceCall.d.ts +18 -0
- package/dist/types/serviceCall.d.ts.map +1 -0
- package/dist/types/serviceCall.js +2 -0
- package/dist/types/serviceCall.js.map +1 -0
- package/dist/types.d.ts +2 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +3 -0
- package/dist/types.js.map +1 -0
- package/dist/utils/config-validations.d.ts +12 -0
- package/dist/utils/config-validations.d.ts.map +1 -0
- package/dist/utils/config-validations.js +43 -0
- package/dist/utils/config-validations.js.map +1 -0
- package/dist/utils/logger.d.ts +24 -0
- package/dist/utils/logger.d.ts.map +1 -0
- package/dist/utils/logger.js +94 -0
- package/dist/utils/logger.js.map +1 -0
- package/dist/wrapper.d.ts +8 -0
- package/dist/wrapper.d.ts.map +1 -0
- package/dist/wrapper.js +301 -0
- package/dist/wrapper.js.map +1 -0
- package/docker-compose.yml +17 -0
- package/docs/CHANGELOG.md +86 -0
- package/docs/LOCAL_CLIENT.md +268 -0
- package/docs/SSE_API.md +341 -0
- package/eslint.config.js +33 -0
- package/n8n/ai-agent-safety-monitor.json +355 -0
- package/openapi.json +166 -0
- package/package.json +51 -0
- package/src/config.ts +260 -0
- package/src/haClient/automations.ts +217 -0
- package/src/haClient/config.ts +41 -0
- package/src/haClient/devices.ts +653 -0
- package/src/haClient/entities.ts +128 -0
- package/src/haClient/events.ts +456 -0
- package/src/haClient/history.ts +68 -0
- package/src/haClient/index.ts +287 -0
- package/src/haClient/request.ts +127 -0
- package/src/haClient/services.ts +45 -0
- package/src/haClient/states.ts +60 -0
- package/src/haClient/updates.ts +58 -0
- package/src/index.ts +99 -0
- package/src/localAI/index.ts +88 -0
- package/src/localAI/prompts.ts +50 -0
- package/src/localAI/providers/base.ts +120 -0
- package/src/localAI/providers/ollama.ts +78 -0
- package/src/localAI/providers/openai.ts +86 -0
- package/src/localAI/types.ts +67 -0
- package/src/resources/index.ts +266 -0
- package/src/server/common.ts +38 -0
- package/src/server/eventSubscription.ts +318 -0
- package/src/server/http.ts +546 -0
- package/src/server/rateLimiter.ts +195 -0
- package/src/server/sse.ts +120 -0
- package/src/server/stdio.ts +22 -0
- package/src/server/stream.ts +113 -0
- package/src/tools/callService.ts +114 -0
- package/src/tools/getEntitiesByDomain.ts +92 -0
- package/src/tools/getState.ts +103 -0
- package/src/tools/getStates.ts +80 -0
- package/src/tools/index.ts +9 -0
- package/src/tools/registry.ts +1521 -0
- package/src/tools/searchEntities.ts +92 -0
- package/src/types/entity.ts +110 -0
- package/src/types/errors.ts +33 -0
- package/src/types/haConfig.ts +7 -0
- package/src/types/index.ts +5 -0
- package/src/types/request.ts +6 -0
- package/src/types/serviceCall.ts +18 -0
- package/src/types.ts +2 -0
- package/src/utils/config-validations.ts +46 -0
- package/src/utils/logger.ts +114 -0
- package/src/wrapper.ts +340 -0
- package/tests/config.test.ts +332 -0
- package/tests/haClient/automations.test.ts +133 -0
- package/tests/haClient/config.test.ts +96 -0
- package/tests/haClient/entities.test.ts +188 -0
- package/tests/haClient/history.test.ts +170 -0
- package/tests/haClient/index.test.ts +221 -0
- package/tests/haClient/request.test.ts +217 -0
- package/tests/haClient/services.test.ts +120 -0
- package/tests/haClient/states.test.ts +119 -0
- package/tests/haClient/updates.test.ts +193 -0
- package/tests/index.test.ts +164 -0
- package/tests/localAI/index.test.ts +168 -0
- package/tests/localAI/ollama.test.ts +187 -0
- package/tests/localAI/openai.test.ts +257 -0
- package/tests/server/common.test.ts +64 -0
- package/tests/server/http.test.ts +326 -0
- package/tests/server/sse.test.ts +240 -0
- package/tests/server/stdio.test.ts +102 -0
- package/tests/server/stream.test.ts +287 -0
- package/tests/tools/registry.test.ts +355 -0
- package/tests/utils/config-validations.test.ts +289 -0
- package/tests/utils/logger.test.ts +327 -0
- package/tsconfig.json +25 -0
- package/vitest.config.ts +26 -0
|
@@ -0,0 +1,289 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Configuration validation utilities tests
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { describe, it, expect } from 'vitest';
|
|
6
|
+
import { isValidBindAddress, isValidOrigin } from '../../src/utils/config-validations.js';
|
|
7
|
+
|
|
8
|
+
describe('isValidBindAddress', () => {
|
|
9
|
+
describe('Valid IPv4 addresses', () => {
|
|
10
|
+
it('should accept localhost 127.0.0.1', () => {
|
|
11
|
+
expect(isValidBindAddress('127.0.0.1')).toBe(true);
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
it('should accept 0.0.0.0 (all interfaces)', () => {
|
|
15
|
+
expect(isValidBindAddress('0.0.0.0')).toBe(true);
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
it('should accept private IP 192.168.1.1', () => {
|
|
19
|
+
expect(isValidBindAddress('192.168.1.1')).toBe(true);
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
it('should accept private IP 10.0.0.1', () => {
|
|
23
|
+
expect(isValidBindAddress('10.0.0.1')).toBe(true);
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it('should accept private IP 172.16.0.1', () => {
|
|
27
|
+
expect(isValidBindAddress('172.16.0.1')).toBe(true);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it('should accept maximum octet values 255.255.255.255', () => {
|
|
31
|
+
expect(isValidBindAddress('255.255.255.255')).toBe(true);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it('should accept public IP 8.8.8.8', () => {
|
|
35
|
+
expect(isValidBindAddress('8.8.8.8')).toBe(true);
|
|
36
|
+
});
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
describe('Invalid IPv4 addresses', () => {
|
|
40
|
+
it('should reject octet > 255', () => {
|
|
41
|
+
expect(isValidBindAddress('256.0.0.1')).toBe(false);
|
|
42
|
+
expect(isValidBindAddress('192.168.256.1')).toBe(false);
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it('should reject negative octets', () => {
|
|
46
|
+
expect(isValidBindAddress('-1.0.0.1')).toBe(false);
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it('should reject too few octets', () => {
|
|
50
|
+
expect(isValidBindAddress('192.168.1')).toBe(false);
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it('should reject too many octets', () => {
|
|
54
|
+
expect(isValidBindAddress('192.168.1.1.1')).toBe(false);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it('should reject non-numeric octets', () => {
|
|
58
|
+
expect(isValidBindAddress('abc.def.ghi.jkl')).toBe(false);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it('should reject empty string', () => {
|
|
62
|
+
expect(isValidBindAddress('')).toBe(false);
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it('should reject hostnames', () => {
|
|
66
|
+
expect(isValidBindAddress('localhost')).toBe(false);
|
|
67
|
+
});
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
describe('Valid IPv6 addresses', () => {
|
|
71
|
+
it('should accept localhost ::1', () => {
|
|
72
|
+
expect(isValidBindAddress('::1')).toBe(true);
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it('should accept all interfaces ::', () => {
|
|
76
|
+
expect(isValidBindAddress('::')).toBe(true);
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
it('should accept full IPv6 address', () => {
|
|
80
|
+
expect(isValidBindAddress('2001:0db8:85a3:0000:0000:8a2e:0370:7334')).toBe(true);
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it('should accept compressed IPv6 address', () => {
|
|
84
|
+
expect(isValidBindAddress('2001:db8::1')).toBe(true);
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
it('should accept link-local address', () => {
|
|
88
|
+
expect(isValidBindAddress('fe80::1')).toBe(true);
|
|
89
|
+
});
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
describe('Invalid IPv6 addresses', () => {
|
|
93
|
+
it('should reject malformed IPv6', () => {
|
|
94
|
+
expect(isValidBindAddress('gggg::1')).toBe(false);
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
it('should use simplified IPv6 validation', () => {
|
|
98
|
+
// Note: The current implementation uses a simplified IPv6 regex
|
|
99
|
+
// For strict validation, consider using a more comprehensive validator
|
|
100
|
+
// This test documents the current behavior
|
|
101
|
+
expect(isValidBindAddress(':::1')).toBe(true); // Simplified regex accepts this
|
|
102
|
+
});
|
|
103
|
+
});
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
describe('isValidOrigin', () => {
|
|
107
|
+
describe('Valid origins', () => {
|
|
108
|
+
it('should accept wildcard *', () => {
|
|
109
|
+
expect(isValidOrigin('*')).toBe(true);
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
it('should accept localhost', () => {
|
|
113
|
+
expect(isValidOrigin('localhost')).toBe(true);
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
it('should accept 127.0.0.1', () => {
|
|
117
|
+
expect(isValidOrigin('127.0.0.1')).toBe(true);
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
it('should accept ::1', () => {
|
|
121
|
+
expect(isValidOrigin('::1')).toBe(true);
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
it('should accept full URL with http', () => {
|
|
125
|
+
expect(isValidOrigin('http://localhost:3000')).toBe(true);
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
it('should accept full URL with https', () => {
|
|
129
|
+
expect(isValidOrigin('https://example.com')).toBe(true);
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
it('should accept URL with port', () => {
|
|
133
|
+
expect(isValidOrigin('http://192.168.0.10:8080')).toBe(true);
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
it('should accept simple hostname', () => {
|
|
137
|
+
expect(isValidOrigin('example.com')).toBe(true);
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
it('should accept hostname with subdomain', () => {
|
|
141
|
+
expect(isValidOrigin('api.example.com')).toBe(true);
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
it('should accept hostname with hyphen', () => {
|
|
145
|
+
expect(isValidOrigin('my-app.example.com')).toBe(true);
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
it('should accept IP address as origin', () => {
|
|
149
|
+
expect(isValidOrigin('192.168.1.100')).toBe(true);
|
|
150
|
+
});
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
describe('Invalid origins', () => {
|
|
154
|
+
it('should reject empty string', () => {
|
|
155
|
+
expect(isValidOrigin('')).toBe(false);
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
it('should reject spaces', () => {
|
|
159
|
+
expect(isValidOrigin(' ')).toBe(false);
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
it('should reject special characters not allowed in hostnames', () => {
|
|
163
|
+
expect(isValidOrigin('invalid@origin')).toBe(false);
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
it('should reject invalid URL protocol', () => {
|
|
167
|
+
expect(isValidOrigin('ftp://example.com')).toBe(true); // URL is valid even if protocol is unusual
|
|
168
|
+
});
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
describe('Edge cases', () => {
|
|
172
|
+
it('should handle URL with path', () => {
|
|
173
|
+
expect(isValidOrigin('http://example.com/path')).toBe(true);
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
it('should handle URL with query string', () => {
|
|
177
|
+
expect(isValidOrigin('http://example.com?query=value')).toBe(true);
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
it('should handle URL with hash', () => {
|
|
181
|
+
expect(isValidOrigin('http://example.com#hash')).toBe(true);
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
it('should handle mixed case', () => {
|
|
185
|
+
expect(isValidOrigin('HTTP://EXAMPLE.COM')).toBe(true);
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
it('should handle IPv6 in URL', () => {
|
|
189
|
+
expect(isValidOrigin('http://[::1]:8080')).toBe(true);
|
|
190
|
+
});
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
describe('Common use cases', () => {
|
|
194
|
+
it('should accept development origins', () => {
|
|
195
|
+
const devOrigins = [
|
|
196
|
+
'http://mcpserver.10.0.0.18.nip.io:3000',
|
|
197
|
+
'http://localhost:8080',
|
|
198
|
+
'http://127.0.0.1:8080',
|
|
199
|
+
'localhost',
|
|
200
|
+
];
|
|
201
|
+
|
|
202
|
+
for (const origin of devOrigins) {
|
|
203
|
+
expect(isValidOrigin(origin)).toBe(true);
|
|
204
|
+
}
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
it('should accept production origins', () => {
|
|
208
|
+
const prodOrigins = [
|
|
209
|
+
'https://example.com',
|
|
210
|
+
'https://www.example.com',
|
|
211
|
+
'https://app.example.com',
|
|
212
|
+
];
|
|
213
|
+
|
|
214
|
+
for (const origin of prodOrigins) {
|
|
215
|
+
expect(isValidOrigin(origin)).toBe(true);
|
|
216
|
+
}
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
it('should accept local network origins', () => {
|
|
220
|
+
const localOrigins = [
|
|
221
|
+
'http://192.168.0.10:8080',
|
|
222
|
+
'http://10.0.0.5:3000',
|
|
223
|
+
'192.168.1.100',
|
|
224
|
+
];
|
|
225
|
+
|
|
226
|
+
for (const origin of localOrigins) {
|
|
227
|
+
expect(isValidOrigin(origin)).toBe(true);
|
|
228
|
+
}
|
|
229
|
+
});
|
|
230
|
+
});
|
|
231
|
+
|
|
232
|
+
describe('Security considerations', () => {
|
|
233
|
+
it('should allow restricting to specific domains', () => {
|
|
234
|
+
const allowedOrigins = [
|
|
235
|
+
'https://app.example.com',
|
|
236
|
+
'https://admin.example.com',
|
|
237
|
+
];
|
|
238
|
+
|
|
239
|
+
const testOrigin = 'https://app.example.com';
|
|
240
|
+
expect(isValidOrigin(testOrigin)).toBe(true);
|
|
241
|
+
expect(allowedOrigins).toContain(testOrigin);
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
it('should support localhost-only configuration', () => {
|
|
245
|
+
const localhostOrigins = [
|
|
246
|
+
'localhost',
|
|
247
|
+
'127.0.0.1',
|
|
248
|
+
'::1',
|
|
249
|
+
];
|
|
250
|
+
|
|
251
|
+
for (const origin of localhostOrigins) {
|
|
252
|
+
expect(isValidOrigin(origin)).toBe(true);
|
|
253
|
+
}
|
|
254
|
+
});
|
|
255
|
+
|
|
256
|
+
it('should support wildcard for development', () => {
|
|
257
|
+
const allowAll = '*';
|
|
258
|
+
expect(isValidOrigin(allowAll)).toBe(true);
|
|
259
|
+
});
|
|
260
|
+
});
|
|
261
|
+
|
|
262
|
+
describe('Integration with config', () => {
|
|
263
|
+
it('should validate comma-separated origins', () => {
|
|
264
|
+
const originsString = 'http://mcpserver.10.0.0.18.nip.io:3000,http://192.168.0.10:8080,localhost';
|
|
265
|
+
const origins = originsString.split(',').map(s => s.trim());
|
|
266
|
+
|
|
267
|
+
for (const origin of origins) {
|
|
268
|
+
expect(isValidOrigin(origin)).toBe(true);
|
|
269
|
+
}
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
it('should detect invalid origin in list', () => {
|
|
273
|
+
const originsString = 'http://mcpserver.10.0.0.18.nip.io:3000,invalid@origin,localhost';
|
|
274
|
+
const origins = originsString.split(',').map(s => s.trim());
|
|
275
|
+
|
|
276
|
+
const invalidOrigin = origins.find(origin => !isValidOrigin(origin));
|
|
277
|
+
expect(invalidOrigin).toBe('invalid@origin');
|
|
278
|
+
});
|
|
279
|
+
|
|
280
|
+
it('should handle whitespace in origin list', () => {
|
|
281
|
+
const originsString = ' http://mcpserver.10.0.0.18.nip.io:3000 , localhost ';
|
|
282
|
+
const origins = originsString.split(',').map(s => s.trim()).filter(Boolean);
|
|
283
|
+
|
|
284
|
+
for (const origin of origins) {
|
|
285
|
+
expect(isValidOrigin(origin)).toBe(true);
|
|
286
|
+
}
|
|
287
|
+
});
|
|
288
|
+
});
|
|
289
|
+
});
|
|
@@ -0,0 +1,327 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Logger tests
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
|
6
|
+
import { initLogger, setLevel, logger } from '../../src/utils/logger.js';
|
|
7
|
+
import type { LogLevel, LogFormat } from '../../src/utils/logger.js';
|
|
8
|
+
|
|
9
|
+
describe('Logger', () => {
|
|
10
|
+
beforeEach(() => {
|
|
11
|
+
// Reset to default configuration
|
|
12
|
+
initLogger('info', 'plain', false);
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
describe('initLogger', () => {
|
|
16
|
+
it('should initialize with default level and format', () => {
|
|
17
|
+
initLogger('info', 'plain', false);
|
|
18
|
+
|
|
19
|
+
// Logger should be initialized without errors
|
|
20
|
+
expect(logger).toBeDefined();
|
|
21
|
+
expect(logger.info).toBeDefined();
|
|
22
|
+
expect(logger.debug).toBeDefined();
|
|
23
|
+
expect(logger.warn).toBeDefined();
|
|
24
|
+
expect(logger.error).toBeDefined();
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it('should support all log levels', () => {
|
|
28
|
+
const levels: LogLevel[] = ['debug', 'info', 'warn', 'error', 'silent'];
|
|
29
|
+
|
|
30
|
+
for (const level of levels) {
|
|
31
|
+
expect(() => initLogger(level, 'plain', false)).not.toThrow();
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it('should support all log formats', () => {
|
|
36
|
+
const formats: LogFormat[] = ['plain', 'json', 'gcp-json'];
|
|
37
|
+
|
|
38
|
+
for (const format of formats) {
|
|
39
|
+
expect(() => initLogger('info', format, false)).not.toThrow();
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it('should configure stderr output when useStderr is true', () => {
|
|
44
|
+
expect(() => initLogger('info', 'plain', true)).not.toThrow();
|
|
45
|
+
|
|
46
|
+
// Logger should be configured to use stderr
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it('should configure stdout output when useStderr is false', () => {
|
|
50
|
+
expect(() => initLogger('info', 'plain', false)).not.toThrow();
|
|
51
|
+
|
|
52
|
+
// Logger should be configured to use stdout
|
|
53
|
+
});
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
describe('setLevel', () => {
|
|
57
|
+
it('should change log level dynamically', () => {
|
|
58
|
+
initLogger('info', 'plain', false);
|
|
59
|
+
|
|
60
|
+
expect(() => setLevel('debug')).not.toThrow();
|
|
61
|
+
expect(() => setLevel('warn')).not.toThrow();
|
|
62
|
+
expect(() => setLevel('error')).not.toThrow();
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it('should accept all valid log levels', () => {
|
|
66
|
+
const levels: LogLevel[] = ['debug', 'info', 'warn', 'error', 'silent'];
|
|
67
|
+
|
|
68
|
+
for (const level of levels) {
|
|
69
|
+
expect(() => setLevel(level)).not.toThrow();
|
|
70
|
+
}
|
|
71
|
+
});
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
describe('Log methods', () => {
|
|
75
|
+
it('should have debug method', () => {
|
|
76
|
+
expect(typeof logger.debug).toBe('function');
|
|
77
|
+
expect(() => logger.debug('Test debug message')).not.toThrow();
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it('should have info method', () => {
|
|
81
|
+
expect(typeof logger.info).toBe('function');
|
|
82
|
+
expect(() => logger.info('Test info message')).not.toThrow();
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
it('should have warn method', () => {
|
|
86
|
+
expect(typeof logger.warn).toBe('function');
|
|
87
|
+
expect(() => logger.warn('Test warn message')).not.toThrow();
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
it('should have error method', () => {
|
|
91
|
+
expect(typeof logger.error).toBe('function');
|
|
92
|
+
expect(() => logger.error('Test error message')).not.toThrow();
|
|
93
|
+
});
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
describe('Log with metadata', () => {
|
|
97
|
+
it('should log with metadata object', () => {
|
|
98
|
+
const metadata = {
|
|
99
|
+
userId: 'user-123',
|
|
100
|
+
requestId: 'req-456',
|
|
101
|
+
duration: 123,
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
expect(() => logger.info('Test message', metadata)).not.toThrow();
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
it('should handle Error objects in metadata', () => {
|
|
108
|
+
const error = new Error('Test error');
|
|
109
|
+
const metadata = {
|
|
110
|
+
error,
|
|
111
|
+
context: 'test',
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
expect(() => logger.error('Error occurred', metadata)).not.toThrow();
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
it('should handle nested objects in metadata', () => {
|
|
118
|
+
const metadata = {
|
|
119
|
+
config: {
|
|
120
|
+
baseUrl: 'http://example.com',
|
|
121
|
+
timeout: 30000,
|
|
122
|
+
},
|
|
123
|
+
user: {
|
|
124
|
+
id: 'user-123',
|
|
125
|
+
name: 'Test User',
|
|
126
|
+
},
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
expect(() => logger.info('Complex metadata', metadata)).not.toThrow();
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
it('should handle arrays in metadata', () => {
|
|
133
|
+
const metadata = {
|
|
134
|
+
entities: ['light.living_room', 'sensor.temperature'],
|
|
135
|
+
tags: ['home', 'automation'],
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
expect(() => logger.info('Array metadata', metadata)).not.toThrow();
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
it('should handle empty metadata', () => {
|
|
142
|
+
expect(() => logger.info('No metadata')).not.toThrow();
|
|
143
|
+
expect(() => logger.info('Empty metadata', {})).not.toThrow();
|
|
144
|
+
});
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
describe('Log formats', () => {
|
|
148
|
+
it('should format plain logs with uppercase level', () => {
|
|
149
|
+
initLogger('info', 'plain', false);
|
|
150
|
+
|
|
151
|
+
// Plain format should use { level: 'INFO' }
|
|
152
|
+
expect(() => logger.info('Plain format test')).not.toThrow();
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
it('should format JSON logs with standard structure', () => {
|
|
156
|
+
initLogger('info', 'json', false);
|
|
157
|
+
|
|
158
|
+
// JSON format should use standard pino JSON
|
|
159
|
+
expect(() => logger.info('JSON format test')).not.toThrow();
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
it('should format GCP JSON logs with severity field', () => {
|
|
163
|
+
initLogger('info', 'gcp-json', false);
|
|
164
|
+
|
|
165
|
+
// GCP format should use { severity: 'INFO' }
|
|
166
|
+
expect(() => logger.info('GCP format test')).not.toThrow();
|
|
167
|
+
});
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
describe('Severity mapping for GCP format', () => {
|
|
171
|
+
it('should map debug to DEBUG', () => {
|
|
172
|
+
const levelToSeverity = {
|
|
173
|
+
debug: 'DEBUG',
|
|
174
|
+
info: 'INFO',
|
|
175
|
+
warn: 'WARNING',
|
|
176
|
+
error: 'ERROR',
|
|
177
|
+
};
|
|
178
|
+
|
|
179
|
+
expect(levelToSeverity.debug).toBe('DEBUG');
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
it('should map info to INFO', () => {
|
|
183
|
+
const levelToSeverity = {
|
|
184
|
+
debug: 'DEBUG',
|
|
185
|
+
info: 'INFO',
|
|
186
|
+
warn: 'WARNING',
|
|
187
|
+
error: 'ERROR',
|
|
188
|
+
};
|
|
189
|
+
|
|
190
|
+
expect(levelToSeverity.info).toBe('INFO');
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
it('should map warn to WARNING', () => {
|
|
194
|
+
const levelToSeverity = {
|
|
195
|
+
debug: 'DEBUG',
|
|
196
|
+
info: 'INFO',
|
|
197
|
+
warn: 'WARNING',
|
|
198
|
+
error: 'ERROR',
|
|
199
|
+
};
|
|
200
|
+
|
|
201
|
+
expect(levelToSeverity.warn).toBe('WARNING');
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
it('should map error to ERROR', () => {
|
|
205
|
+
const levelToSeverity = {
|
|
206
|
+
debug: 'DEBUG',
|
|
207
|
+
info: 'INFO',
|
|
208
|
+
warn: 'WARNING',
|
|
209
|
+
error: 'ERROR',
|
|
210
|
+
};
|
|
211
|
+
|
|
212
|
+
expect(levelToSeverity.error).toBe('ERROR');
|
|
213
|
+
});
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
describe('Error metadata normalization', () => {
|
|
217
|
+
it('should normalize Error to object with message and stack', () => {
|
|
218
|
+
const error = new Error('Test error');
|
|
219
|
+
const normalized = {
|
|
220
|
+
message: error.message,
|
|
221
|
+
stack: error.stack,
|
|
222
|
+
};
|
|
223
|
+
|
|
224
|
+
expect(normalized.message).toBe('Test error');
|
|
225
|
+
expect(normalized.stack).toBeDefined();
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
it('should preserve non-Error values', () => {
|
|
229
|
+
const metadata = {
|
|
230
|
+
string: 'test',
|
|
231
|
+
number: 123,
|
|
232
|
+
boolean: true,
|
|
233
|
+
null: null,
|
|
234
|
+
array: [1, 2, 3],
|
|
235
|
+
object: { key: 'value' },
|
|
236
|
+
};
|
|
237
|
+
|
|
238
|
+
expect(metadata.string).toBe('test');
|
|
239
|
+
expect(metadata.number).toBe(123);
|
|
240
|
+
expect(metadata.boolean).toBe(true);
|
|
241
|
+
expect(metadata.null).toBeNull();
|
|
242
|
+
expect(metadata.array).toEqual([1, 2, 3]);
|
|
243
|
+
expect(metadata.object).toEqual({ key: 'value' });
|
|
244
|
+
});
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
describe('Timestamp format', () => {
|
|
248
|
+
it('should use ISO time format', () => {
|
|
249
|
+
// Pino stdTimeFunctions.isoTime produces ISO 8601 timestamps
|
|
250
|
+
const now = new Date();
|
|
251
|
+
const isoTime = now.toISOString();
|
|
252
|
+
|
|
253
|
+
expect(isoTime).toMatch(/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z$/);
|
|
254
|
+
});
|
|
255
|
+
});
|
|
256
|
+
|
|
257
|
+
describe('Message key', () => {
|
|
258
|
+
it('should use "message" as the message key', () => {
|
|
259
|
+
const logEntry = {
|
|
260
|
+
level: 'INFO',
|
|
261
|
+
time: '2026-01-08T12:00:00.000Z',
|
|
262
|
+
message: 'Test message',
|
|
263
|
+
};
|
|
264
|
+
|
|
265
|
+
expect(logEntry.message).toBe('Test message');
|
|
266
|
+
expect('message' in logEntry).toBe(true);
|
|
267
|
+
});
|
|
268
|
+
});
|
|
269
|
+
|
|
270
|
+
describe('Base fields', () => {
|
|
271
|
+
it('should not include base fields like hostname and pid', () => {
|
|
272
|
+
// baseConfig.base = undefined disables these fields
|
|
273
|
+
const logEntry = {
|
|
274
|
+
level: 'INFO',
|
|
275
|
+
time: '2026-01-08T12:00:00.000Z',
|
|
276
|
+
message: 'Test message',
|
|
277
|
+
};
|
|
278
|
+
|
|
279
|
+
expect('hostname' in logEntry).toBe(false);
|
|
280
|
+
expect('pid' in logEntry).toBe(false);
|
|
281
|
+
expect('name' in logEntry).toBe(false);
|
|
282
|
+
});
|
|
283
|
+
});
|
|
284
|
+
|
|
285
|
+
describe('Stdio mode behavior', () => {
|
|
286
|
+
it('should log to stderr in stdio mode to avoid MCP protocol interference', () => {
|
|
287
|
+
const useStderr = true;
|
|
288
|
+
|
|
289
|
+
initLogger('info', 'plain', useStderr);
|
|
290
|
+
|
|
291
|
+
// Logger should be configured for stderr
|
|
292
|
+
expect(() => logger.info('Stdio mode test')).not.toThrow();
|
|
293
|
+
});
|
|
294
|
+
|
|
295
|
+
it('should log to stdout in HTTP mode', () => {
|
|
296
|
+
const useStderr = false;
|
|
297
|
+
|
|
298
|
+
initLogger('info', 'plain', useStderr);
|
|
299
|
+
|
|
300
|
+
// Logger should be configured for stdout
|
|
301
|
+
expect(() => logger.info('HTTP mode test')).not.toThrow();
|
|
302
|
+
});
|
|
303
|
+
});
|
|
304
|
+
|
|
305
|
+
describe('Environment variable initialization', () => {
|
|
306
|
+
it('should use MCP_SERVER_LOG_LEVEL from environment by default', () => {
|
|
307
|
+
// The default instance reads from process.env.MCP_SERVER_LOG_LEVEL
|
|
308
|
+
const defaultLevel = (process.env.MCP_SERVER_LOG_LEVEL as LogLevel | undefined) ?? 'info';
|
|
309
|
+
|
|
310
|
+
expect(['debug', 'info', 'warn', 'error', 'silent']).toContain(defaultLevel);
|
|
311
|
+
});
|
|
312
|
+
});
|
|
313
|
+
|
|
314
|
+
describe('Multiple logger instances', () => {
|
|
315
|
+
it('should replace previous instance when reinitializing', () => {
|
|
316
|
+
initLogger('info', 'plain', false);
|
|
317
|
+
const firstInit = { ...logger };
|
|
318
|
+
|
|
319
|
+
initLogger('debug', 'json', true);
|
|
320
|
+
const secondInit = { ...logger };
|
|
321
|
+
|
|
322
|
+
// Second initialization should replace the first
|
|
323
|
+
expect(firstInit).toBeDefined();
|
|
324
|
+
expect(secondInit).toBeDefined();
|
|
325
|
+
});
|
|
326
|
+
});
|
|
327
|
+
});
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES2022",
|
|
4
|
+
"module": "ES2022",
|
|
5
|
+
"lib": ["ES2022"],
|
|
6
|
+
"moduleResolution": "node",
|
|
7
|
+
"rootDir": "./src",
|
|
8
|
+
"outDir": "./dist",
|
|
9
|
+
"declaration": true,
|
|
10
|
+
"declarationMap": true,
|
|
11
|
+
"sourceMap": true,
|
|
12
|
+
"strict": true,
|
|
13
|
+
"esModuleInterop": true,
|
|
14
|
+
"skipLibCheck": true,
|
|
15
|
+
"forceConsistentCasingInFileNames": true,
|
|
16
|
+
"resolveJsonModule": true,
|
|
17
|
+
"noUnusedLocals": true,
|
|
18
|
+
"noUnusedParameters": true,
|
|
19
|
+
"noImplicitReturns": true,
|
|
20
|
+
"noFallthroughCasesInSwitch": true,
|
|
21
|
+
"allowSyntheticDefaultImports": true
|
|
22
|
+
},
|
|
23
|
+
"include": ["src/**/*"],
|
|
24
|
+
"exclude": ["node_modules", "dist", "tests"]
|
|
25
|
+
}
|
package/vitest.config.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { defineConfig } from 'vitest/config';
|
|
2
|
+
|
|
3
|
+
export default defineConfig({
|
|
4
|
+
test: {
|
|
5
|
+
globals: true,
|
|
6
|
+
environment: 'node',
|
|
7
|
+
reporters: ['basic'],
|
|
8
|
+
exclude: [
|
|
9
|
+
'**/node_modules/**',
|
|
10
|
+
'**/dist/**',
|
|
11
|
+
'**/example/**',
|
|
12
|
+
],
|
|
13
|
+
coverage: {
|
|
14
|
+
provider: 'v8',
|
|
15
|
+
reporter: ['text', 'json', 'html'],
|
|
16
|
+
exclude: [
|
|
17
|
+
'node_modules/',
|
|
18
|
+
'dist/',
|
|
19
|
+
'example/',
|
|
20
|
+
'tests/',
|
|
21
|
+
'**/*.test.ts',
|
|
22
|
+
'**/*.config.ts',
|
|
23
|
+
],
|
|
24
|
+
},
|
|
25
|
+
},
|
|
26
|
+
});
|