@bedrockio/ai 0.5.0 → 0.5.1

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/CHANGELOG.md CHANGED
@@ -1,3 +1,7 @@
1
+ ## 0.5.1
2
+
3
+ - Added basic api key authorization.
4
+
1
5
  ## 0.5.0
2
6
 
3
7
  - Added `McpServer` and basic handling of using it in tools.
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const SUPPORTED_VERSIONS = ['2025-03-26', '2025-06-18'];
4
4
  const ERROR_INVALID_SESSION = -32000;
5
+ const ERROR_UNAUTHORIZED = -32001;
5
6
  const ERROR_METHOD_NOT_FOUND = -32601;
6
7
  const ERROR_INVALID_REQUEST = -32600;
7
8
  const ERROR_INVALID_PARAMS = -32602;
@@ -17,6 +18,7 @@ class McpServer {
17
18
  return;
18
19
  }
19
20
  this.assertValidTransport(body);
21
+ await this.assertAuthorization(ctx);
20
22
  let result;
21
23
  if (method === 'initialize') {
22
24
  this.setNewSessionId(ctx);
@@ -57,10 +59,21 @@ class McpServer {
57
59
  throw new InvalidRequestError();
58
60
  }
59
61
  }
62
+ async assertAuthorization(ctx) {
63
+ const { apiKeyRequired, isValidApiKey } = this.options;
64
+ const bearer = this.getBearer(ctx);
65
+ if (apiKeyRequired || bearer) {
66
+ const isValid = await isValidApiKey(bearer, ctx);
67
+ if (!isValid) {
68
+ throw new UnauthorizedError();
69
+ }
70
+ }
71
+ }
60
72
  assertValidSession(ctx) {
61
73
  if (!this.hasValidSessionId(ctx)) {
62
74
  throw new InvalidSessionError();
63
75
  }
76
+ ctx.set('content-type', 'application/json; charset=utf-8');
64
77
  }
65
78
  // Calls
66
79
  initialize(body) {
@@ -88,7 +101,7 @@ class McpServer {
88
101
  };
89
102
  }
90
103
  listTools() {
91
- const { tools } = this.options;
104
+ const { tools = [] } = this.options;
92
105
  return {
93
106
  result: {
94
107
  tools: tools.map((tool) => {
@@ -179,6 +192,11 @@ class McpServer {
179
192
  ctx.set('mcp-session-id', sessionId);
180
193
  }
181
194
  }
195
+ // Authorization helpers
196
+ getBearer(ctx) {
197
+ const authorization = ctx.get('authorization') || '';
198
+ return authorization.match(/Bearer (.+)/)?.[1];
199
+ }
182
200
  // Version helpers
183
201
  isSupportedVersion(version) {
184
202
  return SUPPORTED_VERSIONS.includes(version);
@@ -197,6 +215,18 @@ class InvalidRequestError extends Error {
197
215
  };
198
216
  }
199
217
  }
218
+ class UnauthorizedError extends Error {
219
+ status = 401;
220
+ toJSON() {
221
+ return {
222
+ jsonrpc: '2.0',
223
+ error: {
224
+ code: ERROR_UNAUTHORIZED,
225
+ message: 'Unauthorized',
226
+ },
227
+ };
228
+ }
229
+ }
200
230
  class InvalidSessionError extends Error {
201
231
  status = 404;
202
232
  toJSON() {
@@ -1,5 +1,6 @@
1
1
  const SUPPORTED_VERSIONS = ['2025-03-26', '2025-06-18'];
2
2
  const ERROR_INVALID_SESSION = -32000;
3
+ const ERROR_UNAUTHORIZED = -32001;
3
4
  const ERROR_METHOD_NOT_FOUND = -32601;
4
5
  const ERROR_INVALID_REQUEST = -32600;
5
6
  const ERROR_INVALID_PARAMS = -32602;
@@ -15,6 +16,7 @@ export default class McpServer {
15
16
  return;
16
17
  }
17
18
  this.assertValidTransport(body);
19
+ await this.assertAuthorization(ctx);
18
20
  let result;
19
21
  if (method === 'initialize') {
20
22
  this.setNewSessionId(ctx);
@@ -55,10 +57,21 @@ export default class McpServer {
55
57
  throw new InvalidRequestError();
56
58
  }
57
59
  }
60
+ async assertAuthorization(ctx) {
61
+ const { apiKeyRequired, isValidApiKey } = this.options;
62
+ const bearer = this.getBearer(ctx);
63
+ if (apiKeyRequired || bearer) {
64
+ const isValid = await isValidApiKey(bearer, ctx);
65
+ if (!isValid) {
66
+ throw new UnauthorizedError();
67
+ }
68
+ }
69
+ }
58
70
  assertValidSession(ctx) {
59
71
  if (!this.hasValidSessionId(ctx)) {
60
72
  throw new InvalidSessionError();
61
73
  }
74
+ ctx.set('content-type', 'application/json; charset=utf-8');
62
75
  }
63
76
  // Calls
64
77
  initialize(body) {
@@ -86,7 +99,7 @@ export default class McpServer {
86
99
  };
87
100
  }
88
101
  listTools() {
89
- const { tools } = this.options;
102
+ const { tools = [] } = this.options;
90
103
  return {
91
104
  result: {
92
105
  tools: tools.map((tool) => {
@@ -177,6 +190,11 @@ export default class McpServer {
177
190
  ctx.set('mcp-session-id', sessionId);
178
191
  }
179
192
  }
193
+ // Authorization helpers
194
+ getBearer(ctx) {
195
+ const authorization = ctx.get('authorization') || '';
196
+ return authorization.match(/Bearer (.+)/)?.[1];
197
+ }
180
198
  // Version helpers
181
199
  isSupportedVersion(version) {
182
200
  return SUPPORTED_VERSIONS.includes(version);
@@ -194,6 +212,18 @@ class InvalidRequestError extends Error {
194
212
  };
195
213
  }
196
214
  }
215
+ class UnauthorizedError extends Error {
216
+ status = 401;
217
+ toJSON() {
218
+ return {
219
+ jsonrpc: '2.0',
220
+ error: {
221
+ code: ERROR_UNAUTHORIZED,
222
+ message: 'Unauthorized',
223
+ },
224
+ };
225
+ }
226
+ }
197
227
  class InvalidSessionError extends Error {
198
228
  status = 404;
199
229
  toJSON() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bedrockio/ai",
3
- "version": "0.5.0",
3
+ "version": "0.5.1",
4
4
  "description": "Bedrock wrapper for common AI chatbots.",
5
5
  "type": "module",
6
6
  "scripts": {
@@ -15,6 +15,7 @@ export default class McpServer {
15
15
  }>;
16
16
  validateOptions(options: any): void;
17
17
  assertValidTransport(body: any): void;
18
+ assertAuthorization(ctx: any): Promise<void>;
18
19
  assertValidSession(ctx: any): void;
19
20
  initialize(body: any): {
20
21
  error: {
@@ -93,6 +94,7 @@ export default class McpServer {
93
94
  getSessionId(ctx: any): any;
94
95
  hasValidSessionId(ctx: any): boolean;
95
96
  setNewSessionId(ctx: any): void;
97
+ getBearer(ctx: any): any;
96
98
  isSupportedVersion(version: any): boolean;
97
99
  }
98
100
  //# sourceMappingURL=McpServer.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"McpServer.d.ts","sourceRoot":"","sources":["../src/McpServer.js"],"names":[],"mappings":"AAOA;IACE,0BAGC;IADC,YAAsB;IAGxB;;;;;;;;;;;OAkCC;IAID,oCAOC;IAED,sCAKC;IAED,mCAIC;IAID;;;;;;;;;;;;;;;;;;;;MAmBC;IAED;;MAIC;IAED;;;;MAUC;IAED;;;;;;;;;;;;OAOC;IAED;;;;;;;OAaC;IAED;;;;;MAOC;IAID;;;;;MAOC;IAED;;;;;;;;;MAWC;IAID,wBAKC;IAED,4BAEC;IAID,4BAEC;IAED,qCAOC;IAED,gCAKC;IAID,0CAEC;CACF"}
1
+ {"version":3,"file":"McpServer.d.ts","sourceRoot":"","sources":["../src/McpServer.js"],"names":[],"mappings":"AAQA;IACE,0BAGC;IADC,YAAsB;IAGxB;;;;;;;;;;;OAmCC;IAID,oCAOC;IAED,sCAKC;IAED,6CAUC;IAED,mCAKC;IAID;;;;;;;;;;;;;;;;;;;;MAmBC;IAED;;MAIC;IAED;;;;MAUC;IAED;;;;;;;;;;;;OAOC;IAED;;;;;;;OAaC;IAED;;;;;MAOC;IAID;;;;;MAOC;IAED;;;;;;;;;MAWC;IAID,wBAKC;IAED,4BAEC;IAID,4BAEC;IAED,qCAOC;IAED,gCAKC;IAID,yBAGC;IAID,0CAEC;CACF"}