@creature-ai/sdk 0.1.16 → 0.1.17

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.
@@ -207,10 +207,10 @@ interface ToolContext {
207
207
  */
208
208
  instanceId: string;
209
209
  /**
210
- * Creature App Token for identity retrieval.
211
- * ONLY present when:
212
- * 1. App opted into Creature-managed auth (`auth: { creatureManaged: true }`)
213
- * 2. Tool call originated from Creature host
210
+ * Creature token for identity retrieval.
211
+ * Present when either:
212
+ * 1. Tool call originated from Creature host (injected via `_creatureToken` arg)
213
+ * 2. Tool call includes OAuth bearer token in Authorization header (ChatGPT/other hosts)
214
214
  *
215
215
  * Use `getIdentity(context.creatureToken)` to retrieve user identity
216
216
  * for multi-tenant data access.
@@ -6786,6 +6786,7 @@ var require_dist = __commonJS({
6786
6786
 
6787
6787
  // src/server/app.ts
6788
6788
  import { randomUUID } from "crypto";
6789
+ import { AsyncLocalStorage } from "async_hooks";
6789
6790
  import path2 from "path";
6790
6791
  import { fileURLToPath } from "url";
6791
6792
 
@@ -13738,6 +13739,15 @@ var WebSocketManager = class {
13738
13739
  };
13739
13740
 
13740
13741
  // src/server/app.ts
13742
+ var requestContextStorage = new AsyncLocalStorage();
13743
+ var extractBearerToken = (authHeader) => {
13744
+ if (!authHeader) return void 0;
13745
+ const trimmed = authHeader.trim();
13746
+ if (trimmed.toLowerCase().startsWith("bearer ")) {
13747
+ return trimmed.slice(7).trim();
13748
+ }
13749
+ return void 0;
13750
+ };
13741
13751
  var App = class {
13742
13752
  // ==========================================================================
13743
13753
  // Private Properties
@@ -14520,38 +14530,42 @@ var App = class {
14520
14530
  }
14521
14531
  async handleMcpPost(req, res) {
14522
14532
  const transportSessionId = req.headers["mcp-session-id"];
14523
- try {
14524
- let transport;
14525
- if (transportSessionId && this.transports.has(transportSessionId)) {
14526
- transport = this.transports.get(transportSessionId);
14527
- } else if (!transportSessionId && isInitializeRequest2(req.body)) {
14528
- const clientName = req.body?.params?.clientInfo?.name;
14529
- this.hostSupportsMultiInstance = clientName === "creature";
14530
- console.log(`[MCP] Client: ${clientName}, multiInstance support: ${this.hostSupportsMultiInstance}`);
14531
- transport = this.createTransport();
14532
- const server = this.createMcpServer();
14533
- await server.connect(transport);
14533
+ const authorizationHeader = req.headers["authorization"];
14534
+ const context = { authorizationHeader };
14535
+ await requestContextStorage.run(context, async () => {
14536
+ try {
14537
+ let transport;
14538
+ if (transportSessionId && this.transports.has(transportSessionId)) {
14539
+ transport = this.transports.get(transportSessionId);
14540
+ } else if (!transportSessionId && isInitializeRequest2(req.body)) {
14541
+ const clientName = req.body?.params?.clientInfo?.name;
14542
+ this.hostSupportsMultiInstance = clientName === "creature";
14543
+ console.log(`[MCP] Client: ${clientName}, multiInstance support: ${this.hostSupportsMultiInstance}`);
14544
+ transport = this.createTransport();
14545
+ const server = this.createMcpServer();
14546
+ await server.connect(transport);
14547
+ await transport.handleRequest(req, res, req.body);
14548
+ return;
14549
+ } else {
14550
+ res.status(400).json({
14551
+ jsonrpc: "2.0",
14552
+ error: { code: -32e3, message: "Bad Request: No valid transport session ID" },
14553
+ id: null
14554
+ });
14555
+ return;
14556
+ }
14534
14557
  await transport.handleRequest(req, res, req.body);
14535
- return;
14536
- } else {
14537
- res.status(400).json({
14538
- jsonrpc: "2.0",
14539
- error: { code: -32e3, message: "Bad Request: No valid transport session ID" },
14540
- id: null
14541
- });
14542
- return;
14543
- }
14544
- await transport.handleRequest(req, res, req.body);
14545
- } catch (error) {
14546
- console.error("Error:", error);
14547
- if (!res.headersSent) {
14548
- res.status(500).json({
14549
- jsonrpc: "2.0",
14550
- error: { code: -32603, message: "Internal server error" },
14551
- id: null
14552
- });
14558
+ } catch (error) {
14559
+ console.error("Error:", error);
14560
+ if (!res.headersSent) {
14561
+ res.status(500).json({
14562
+ jsonrpc: "2.0",
14563
+ error: { code: -32603, message: "Internal server error" },
14564
+ id: null
14565
+ });
14566
+ }
14553
14567
  }
14554
- }
14568
+ });
14555
14569
  }
14556
14570
  async handleMcpGet(req, res) {
14557
14571
  const transportSessionId = req.headers["mcp-session-id"];
@@ -14687,7 +14701,11 @@ var App = class {
14687
14701
  },
14688
14702
  async (args) => {
14689
14703
  try {
14690
- const creatureToken = args._creatureToken;
14704
+ let creatureToken = args._creatureToken;
14705
+ if (!creatureToken) {
14706
+ const reqContext = requestContextStorage.getStore();
14707
+ creatureToken = extractBearerToken(reqContext?.authorizationHeader);
14708
+ }
14691
14709
  const { _creatureToken: _, ...cleanArgs } = args;
14692
14710
  const input = config.input ? config.input.parse(cleanArgs) : cleanArgs;
14693
14711
  let instanceId;