akanjs 3.3.0-rc.67 → 3.3.0-rc.69

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.
Files changed (45) hide show
  1. package/package.json +1 -1
  2. package/server/akanServer.ts +23 -2
  3. package/server/mcp/McpAuth.ts +12 -2
  4. package/server/mcp/McpDispatcher.ts +20 -3
  5. package/server/mcp/McpFoundTools.ts +69 -0
  6. package/server/mcp/McpRouter.ts +164 -167
  7. package/server/mcp/McpSearch.ts +161 -0
  8. package/server/mcp/McpShelves.ts +95 -0
  9. package/server/mcp/McpWire.ts +92 -0
  10. package/server/mcp/index.ts +2 -0
  11. package/server/oauth/OAuthAuthorize.ts +8 -3
  12. package/service/agentLoop/agentLoop.ts +20 -6
  13. package/service/agentLoop/agentLoopTypes.ts +45 -0
  14. package/service/agentLoop/loopCompaction.ts +7 -1
  15. package/service/agentLoop/loopRequest.ts +25 -2
  16. package/service/agentLoop/loopToolBudget.ts +37 -0
  17. package/service/agentLoop/loopToolSearch.ts +85 -0
  18. package/service/agentLoop/loopToolkits.ts +201 -28
  19. package/service/predefinedAdaptor/llm.adaptor.ts +2 -0
  20. package/signal/mcp/McpDocument.ts +13 -4
  21. package/types/server/akanServer.d.ts +13 -1
  22. package/types/server/logging/rotatingLogWriter.d.ts +1 -1
  23. package/types/server/mcp/McpAuth.d.ts +2 -0
  24. package/types/server/mcp/McpDispatcher.d.ts +7 -2
  25. package/types/server/mcp/McpFoundTools.d.ts +14 -0
  26. package/types/server/mcp/McpRouter.d.ts +6 -0
  27. package/types/server/mcp/McpSearch.d.ts +40 -0
  28. package/types/server/mcp/McpShelves.d.ts +30 -0
  29. package/types/server/mcp/McpWire.d.ts +39 -0
  30. package/types/server/mcp/index.d.ts +2 -0
  31. package/types/server/oauth/OAuthAuthorize.d.ts +5 -2
  32. package/types/service/agentLoop/agentLoopTypes.d.ts +43 -0
  33. package/types/service/agentLoop/loopCompaction.d.ts +1 -1
  34. package/types/service/agentLoop/loopToolBudget.d.ts +8 -0
  35. package/types/service/agentLoop/loopToolSearch.d.ts +13 -0
  36. package/types/service/agentLoop/loopToolkits.d.ts +7 -2
  37. package/types/service/predefinedAdaptor/llm.adaptor.d.ts +2 -0
  38. package/types/signal/mcp/McpDocument.d.ts +3 -0
  39. package/types/vendor/@akanjs/native/packages/cli/src/lib/maven.d.ts +2 -2
  40. package/vendor/@akanjs/native/README.ko.md +1 -1
  41. package/vendor/@akanjs/native/README.md +1 -1
  42. package/vendor/@akanjs/native/THIRD_PARTY_NOTICES.md +78 -56
  43. package/vendor/@akanjs/native/native/android/maven.lock.json +462 -1
  44. package/vendor/@akanjs/native/packages/cli/src/lib/maven.ts +12 -4
  45. package/vendor/@akanjs/native/packages/cli/src/platforms/android.ts +2 -2
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "akanjs",
3
- "version": "3.3.0-rc.67",
3
+ "version": "3.3.0-rc.69",
4
4
  "sourceType": "module",
5
5
  "type": "module",
6
6
  "publishConfig": {
@@ -24,7 +24,15 @@ import { ParentChannel } from "./lifecycle/parentChannel";
24
24
  import { ShutdownManager } from "./lifecycle/shutdownManager";
25
25
  import { StartGuard } from "./lifecycle/startGuard";
26
26
  import { ServerLogging } from "./logging/serverLogging";
27
- import { AgentTools, type McpAuthOption, type McpRateLimitOption, type McpRouter } from "./mcp";
27
+ import {
28
+ AgentTools,
29
+ type McpAuthOption,
30
+ type McpRateLimitOption,
31
+ type McpRouter,
32
+ type McpSearchOption,
33
+ type McpShelfOption,
34
+ type McpStructuredContentMode,
35
+ } from "./mcp";
28
36
  import { OtlpExporter } from "./otlp/otlpExporter";
29
37
  import { WebProxyRunner } from "./proxy";
30
38
  import { SignalResolver } from "./resolver";
@@ -112,6 +120,18 @@ export interface McpServerOption {
112
120
  * `AKAN_MCP_MODERN`.
113
121
  */
114
122
  modern?: boolean;
123
+ /**
124
+ * More listings, each at `<path>/<name>` with only the named endpoints and its own `instructions`, for a client that
125
+ * needs a few tools; same auth, guards and rate limit, and its own OAuth resource. Merged by name across `setMcp`.
126
+ */
127
+ shelves?: Record<string, McpShelfOption>;
128
+ /**
129
+ * The root lists only `listed`, plus `findTools`, which adds the tools it finds to that caller's listing (announced
130
+ * with `notifications/tools/list_changed`), and `readTool`, which runs any read-only tool by name.
131
+ */
132
+ search?: McpSearchOption;
133
+ /** `declared` ships `structuredContent` only for a tool publishing an `outputSchema`; `always` (default) for every one. */
134
+ structuredContent?: McpStructuredContentMode;
115
135
  }
116
136
 
117
137
  export interface AkanServerConsoleInfo {
@@ -269,7 +289,7 @@ export class AkanServer {
269
289
  const requested = typeof mcp === "boolean" ? mcp : (mcp.enabled ?? this.mcp);
270
290
  this.mcp = requested && !ServerConfig.isEnvOff("AKAN_MCP", "AKAN_PUBLIC_MCP");
271
291
  if (typeof mcp === "boolean") return this;
272
- const { enabled: _enabled, readOnly, auth, excludeSignals, excludeEndpoints, ...rest } = mcp;
292
+ const { enabled: _enabled, readOnly, auth, excludeSignals, excludeEndpoints, shelves, ...rest } = mcp;
273
293
  if (readOnly !== undefined) this.mcpReadOnly = readOnly;
274
294
  if (auth) this.mcpAuth = { ...this.mcpAuth, ...ServerConfig.defined(auth) };
275
295
  const added = (current: string[] | undefined, names: string[] | undefined) =>
@@ -279,6 +299,7 @@ export class AkanServer {
279
299
  ...ServerConfig.defined(rest),
280
300
  excludeSignals: added(this.mcpOption.excludeSignals, excludeSignals),
281
301
  excludeEndpoints: added(this.mcpOption.excludeEndpoints, excludeEndpoints),
302
+ shelves: shelves ? { ...this.mcpOption.shelves, ...shelves } : this.mcpOption.shelves,
282
303
  });
283
304
  return this;
284
305
  }
@@ -17,6 +17,8 @@ export interface McpAuthOption {
17
17
 
18
18
  export interface McpAuthProps extends McpAuthOption {
19
19
  path: string;
20
+ /** A shelf's root: its tokens pass here too, as a shelf serves a subset of the root's tools under the same guards. */
21
+ parent?: McpAuth;
20
22
  }
21
23
 
22
24
  interface McpAuthFailure {
@@ -37,7 +39,7 @@ export class McpAuth {
37
39
  createRoutes(): HttpRoutes {
38
40
  const handler = { GET: (req: Request) => this.#metadata(req) };
39
41
  return {
40
- [McpAuth.wellKnownPath]: handler,
42
+ ...(this.#props.parent ? {} : { [McpAuth.wellKnownPath]: handler }),
41
43
  [`${McpAuth.wellKnownPath}${this.#props.path}`]: handler,
42
44
  };
43
45
  }
@@ -90,7 +92,10 @@ export class McpAuth {
90
92
 
91
93
  if (this.#props.authorizationServers?.length && !audience.length)
92
94
  return { status: 401, error: "invalid_token", description: "The access token names no resource." };
93
- if (audience.length && !(this.answersTo(req) && audience.includes(this.#resource(req))))
95
+ if (
96
+ audience.length &&
97
+ !(this.answersTo(req) && this.#resources(req).some((resource) => audience.includes(resource)))
98
+ )
94
99
  return { status: 401, error: "invalid_token", description: "The access token was issued for another resource." };
95
100
  const missing = this.#missingScopes(claims);
96
101
 
@@ -139,6 +144,11 @@ export class McpAuth {
139
144
  return this.#props.resource ?? new URL(this.#props.path, this.publicOrigin(req)).href;
140
145
  }
141
146
 
147
+ #resources(req: Request): string[] {
148
+ const parent = this.#props.parent;
149
+ return parent ? [this.#resource(req), parent.#resource(req)] : [this.#resource(req)];
150
+ }
151
+
142
152
  publicOrigin(req: Request) {
143
153
  if (this.#props.resource) {
144
154
  try {
@@ -15,7 +15,13 @@ import type { Endpoint, EndpointCls } from "../../signal/endpoint";
15
15
  import type { EndpointInfo } from "../../signal/endpointInfo";
16
16
  import { Exception, isExceptionLike } from "../../signal/exception";
17
17
  import type { GuardCls } from "../../signal/guard";
18
- import { MCP_APP_META, McpDocument, type McpExposedEndpoint, type McpToolResult } from "../../signal/mcp";
18
+ import {
19
+ MCP_APP_META,
20
+ McpDocument,
21
+ type McpExposedEndpoint,
22
+ type McpOutputSchemaMode,
23
+ type McpToolResult,
24
+ } from "../../signal/mcp";
19
25
  import type { MiddlewareCls } from "../../signal/middleware";
20
26
  import { SignalFailure } from "../../signal/SignalFailure";
21
27
  import { SignalContext } from "../../signal/signalContext";
@@ -35,8 +41,13 @@ interface McpDispatcherProps {
35
41
  middleware: Map<string, MiddlewareCls>;
36
42
  language?: string;
37
43
  legacyTextBlock?: boolean;
44
+ outputSchema?: McpOutputSchemaMode;
45
+ structuredContent?: McpStructuredContentMode;
38
46
  }
39
47
 
48
+ /** `declared`: `structuredContent` only for a tool publishing an `outputSchema`, else the text block alone. */
49
+ export type McpStructuredContentMode = "always" | "declared";
50
+
40
51
  export type McpAnswer = { value: unknown } | { images: ImageResultValue[] } | { error: string; code: string };
41
52
 
42
53
  export class McpDispatcher {
@@ -58,7 +69,7 @@ export class McpDispatcher {
58
69
  exposed: McpExposedEndpoint,
59
70
  args: Record<string, unknown>,
60
71
  req: Request,
61
- { forView = false }: { forView?: boolean } = {},
72
+ { forView = false, structured = false }: { forView?: boolean; structured?: boolean } = {},
62
73
  ): Promise<McpToolResult> {
63
74
  const found = this.#index().get(exposed.key);
64
75
  if (!found) return McpDispatcher.#failure(`Tool "${exposed.key}" is declared but not mounted on this server.`);
@@ -70,7 +81,8 @@ export class McpDispatcher {
70
81
  return image.isError ? image : { ...image, ...view };
71
82
  }
72
83
  const value = McpDispatcher.#readable(exposed, wire);
73
- const structuredContent = McpDocument.structuredContent(exposed.endpoint, value);
84
+ const keep = forView || structured || this.#keepsStructured(exposed);
85
+ const structuredContent = keep ? McpDocument.structuredContent(exposed.endpoint, value) : undefined;
74
86
  return {
75
87
  content: this.#content(structuredContent, value),
76
88
  ...(structuredContent === undefined ? {} : { structuredContent }),
@@ -218,6 +230,11 @@ export class McpDispatcher {
218
230
  return undefined;
219
231
  }
220
232
 
233
+ #keepsStructured({ endpoint }: McpExposedEndpoint) {
234
+ if (this.#props.structuredContent !== "declared") return true;
235
+ return McpDocument.publishesOutputSchema(endpoint, this.#props.outputSchema);
236
+ }
237
+
221
238
  #content(structuredContent: unknown, value: unknown): McpToolResult["content"] {
222
239
  if (structuredContent !== undefined && this.#props.legacyTextBlock === false)
223
240
  return [{ type: "text", text: McpDispatcher.structuredNote }];
@@ -0,0 +1,69 @@
1
+ import { dayjs } from "akanjs/base";
2
+ import { Logger } from "akanjs/common";
3
+ import type { CacheAdaptor } from "akanjs/service";
4
+
5
+ export type McpFoundToolsStore = Pick<CacheAdaptor, "get" | "set">;
6
+
7
+ export class McpFoundTools {
8
+ static readonly logger = new Logger("McpFoundTools");
9
+ static readonly ttlMs = 24 * 60 * 60 * 1000;
10
+ static readonly perCaller = 64;
11
+ static readonly memoryCallers = 5_000;
12
+ static readonly #topic = "mcpFound";
13
+
14
+ readonly #store: McpFoundToolsStore | null;
15
+ readonly #memory = new Map<string, { names: string[]; expiresAt: number }>();
16
+ #failing = false;
17
+
18
+ constructor(store: McpFoundToolsStore | null) {
19
+ this.#store = store;
20
+ }
21
+
22
+ async of(caller: string): Promise<string[]> {
23
+ if (this.#store) {
24
+ try {
25
+ const names = await this.#store.get<string[]>(McpFoundTools.#topic, caller);
26
+ this.#failing = false;
27
+ return Array.isArray(names) ? names.filter((name) => typeof name === "string") : [];
28
+ } catch (error) {
29
+ this.#warn(error);
30
+ }
31
+ }
32
+ const held = this.#memory.get(caller);
33
+ if (!held) return [];
34
+ if (held.expiresAt > Date.now()) return held.names;
35
+ this.#memory.delete(caller);
36
+ return [];
37
+ }
38
+
39
+ /** Whether any of `names` was not found before, which is when the caller's listing changed. */
40
+ async add(caller: string, names: string[]): Promise<boolean> {
41
+ const before = await this.of(caller);
42
+ const added = names.filter((name) => !before.includes(name));
43
+ if (!added.length) return false;
44
+ const kept = [...before, ...added].slice(-McpFoundTools.perCaller);
45
+ if (this.#store) {
46
+ try {
47
+ await this.#store.set(McpFoundTools.#topic, caller, kept, {
48
+ expireAt: dayjs().add(McpFoundTools.ttlMs, "millisecond"),
49
+ });
50
+ this.#failing = false;
51
+ return true;
52
+ } catch (error) {
53
+ this.#warn(error);
54
+ }
55
+ }
56
+ this.#memory.delete(caller);
57
+ this.#memory.set(caller, { names: kept, expiresAt: Date.now() + McpFoundTools.ttlMs });
58
+ const oldest = this.#memory.keys().next().value;
59
+ if (this.#memory.size > McpFoundTools.memoryCallers && oldest !== undefined) this.#memory.delete(oldest);
60
+ return true;
61
+ }
62
+
63
+ #warn(error: unknown) {
64
+ const first = !this.#failing;
65
+ this.#failing = true;
66
+ if (first)
67
+ McpFoundTools.logger.warn(`MCP keeps found tools in this process while the cache fails: ${String(error)}`);
68
+ }
69
+ }