openloops 0.7.2 → 0.7.3

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 (69) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/dist/services/mcp-servers/admin/use-cases/create-internal-mcp.use-case.d.ts +1 -1
  3. package/dist/services/mcp-servers/admin/use-cases/create-internal-mcp.use-case.js +2 -2
  4. package/dist/services/mcp-servers/admin/use-cases/create-internal-mcp.use-case.mjs +2 -2
  5. package/dist/services/mcp-servers/admin/use-cases/update-internal-mcp.use-case.d.ts +1 -1
  6. package/dist/services/mcp-servers/admin/use-cases/update-internal-mcp.use-case.js +2 -2
  7. package/dist/services/mcp-servers/admin/use-cases/update-internal-mcp.use-case.mjs +2 -2
  8. package/dist/services/mcp-servers/entities/mcpserver.entity.d.ts +1 -3
  9. package/dist/services/mcp-servers/entities/mcpserver.entity.js +3 -8
  10. package/dist/services/mcp-servers/entities/mcpserver.entity.mjs +3 -8
  11. package/dist/services/mcp-servers/internal.d.ts +0 -1
  12. package/dist/services/mcp-servers/internal.js +1 -1
  13. package/dist/services/mcp-servers/internal.mjs +1 -1
  14. package/dist/services/mcp-servers/use-cases/update-mcpserver.use-case.js +1 -1
  15. package/dist/services/mcp-servers/use-cases/update-mcpserver.use-case.mjs +1 -1
  16. package/docs/automations.md +1 -0
  17. package/docs/mcp-servers.md +3 -4
  18. package/examples/authentication/login.ts +5 -0
  19. package/examples/automations/create-automation.ts +5 -0
  20. package/examples/automations/create-event-automation.ts +5 -0
  21. package/examples/automations/list-automation-runs.ts +6 -0
  22. package/examples/automations/list-automations.ts +3 -0
  23. package/examples/automations/preview-automation-runs.ts +3 -0
  24. package/examples/automations/receive-automation-event.ts +5 -0
  25. package/examples/automations/revoke-automation-event-token.ts +6 -0
  26. package/examples/automations/run-automation-now.ts +3 -0
  27. package/examples/automations/run-automation-worker.ts +7 -0
  28. package/examples/automations/update-automation.ts +6 -0
  29. package/examples/chats/delete-chat.ts +6 -0
  30. package/examples/chats/get-chat-by-id.ts +6 -0
  31. package/examples/chats/list-autonomous-chats.ts +3 -0
  32. package/examples/chats/list-chats.ts +3 -0
  33. package/examples/guardrails/custom-guard.ts +5 -0
  34. package/examples/loops/custom-loop.ts +8 -0
  35. package/examples/mcps/create-mcp.ts +5 -0
  36. package/examples/mcps/decide-mcp.ts +6 -0
  37. package/examples/mcps/delete-mcp.ts +6 -0
  38. package/examples/mcps/get-mcp-by-id.ts +3 -0
  39. package/examples/mcps/internal-mcp-admin.ts +24 -1
  40. package/examples/mcps/internal-mcp.ts +14 -0
  41. package/examples/mcps/list-all-mcps.ts +3 -0
  42. package/examples/mcps/list-user-mcps.ts +3 -0
  43. package/examples/mcps/update-mcp.ts +5 -0
  44. package/examples/mcps/verify-mcp.ts +3 -0
  45. package/examples/run-agent/interrupt-agent.ts +3 -0
  46. package/examples/run-agent/run-agent-autonomous.ts +3 -0
  47. package/examples/run-agent/run-agent-strict-mode.ts +3 -0
  48. package/examples/run-agent/run-agent-with-confirmation.ts +6 -0
  49. package/examples/run-agent/run-agent-with-fallback-model.ts +3 -0
  50. package/examples/run-agent/run-agent-with-guards.ts +3 -0
  51. package/examples/run-agent/run-agent-with-hooks.ts +6 -0
  52. package/examples/run-agent/run-agent-with-identity.ts +3 -0
  53. package/examples/run-agent/run-agent-with-model.ts +3 -0
  54. package/examples/run-agent/run-agent-with-slack.ts +3 -0
  55. package/examples/run-agent/run-agent-with-tools.ts +3 -0
  56. package/examples/tools/basic-tool.ts +3 -0
  57. package/examples/tools/destructive-tool.ts +3 -0
  58. package/examples/tools/web-search-tool.ts +3 -0
  59. package/examples/usage/get-chat-usage.ts +3 -0
  60. package/examples/usage/get-message-usage.ts +3 -0
  61. package/examples/usage/get-usage-stats-by-range.ts +3 -0
  62. package/examples/usage/get-usage-stats.ts +3 -0
  63. package/examples/users/create-user.ts +3 -0
  64. package/examples/users/delete-user.ts +6 -0
  65. package/examples/users/get-user-by-id.ts +3 -0
  66. package/examples/users/list-users.ts +3 -0
  67. package/examples/users/set-default-model.ts +6 -0
  68. package/examples/users/update-user.ts +3 -0
  69. package/package.json +1 -1
package/CHANGELOG.md CHANGED
@@ -11,7 +11,7 @@ This version breaks the API of the internal MCP servers in automations. Before,
11
11
  - **Internal MCP servers stored in the database**, managed from a back office. `createInternalMcp`, `updateInternalMcp`, `getInternalMcpById`, `listInternalMcps` and `deleteInternalMcp` in the new entry point `openloops/mcps/admin`, all for admins only. The agent loads the enabled ones in every run next to the ones declared in code, with the same rules: trusted, no manifest review, destructive tools still ask. `{{token}}` in a header uses the token of the run, a token written in the header is sent as it is. Read [MCP servers](docs/mcp-servers.md#internal-servers-stored-in-the-database).
12
12
  - `internal` and `enabled` in `McpServerProps`. A record without them is a normal server, so no migration is needed.
13
13
  - A unique index on the name of the internal servers: run `ensureIndexes`.
14
- - `autonomousToken` in the internal MCP servers, declared in code (`InternalMcp`) or stored (`createInternalMcp` and `updateInternalMcp`). It is the token of a service account, used for `{{token}}` only in an autonomous run that brings no token for that server, and then the agent sends the id of the user in `X-User-Id`. An interactive run never uses it. It is temporary, until the integrations give the runner the token of the owner.
14
+ - `autonomousToken` in the internal MCP servers declared in code (`InternalMcp`), not in the stored ones. It is the token of a service account, used for `{{token}}` only in an autonomous run that brings no token for that server, and then the agent sends the id of the user in `X-User-Id`. An interactive run never uses it. It is temporary, until the integrations give the runner the token of the owner.
15
15
  - `environment` in `McpServerProps`, an optional text for every MCP server, internal or not, to say which environment it belongs to. It is only a label. The functions of `openloops/mcps` and of `openloops/mcps/admin` accept it, and a blank text removes it.
16
16
 
17
17
  ### Removed
@@ -2,7 +2,7 @@ import { BaseUseCase, BaseUseCaseParams } from '../../../../common/base/base.use
2
2
  import { McpServerProps } from '../../entities';
3
3
  import { McpServerRepository } from '../../repositories';
4
4
  export interface Params extends BaseUseCaseParams {
5
- payload?: Pick<McpServerProps, 'name' | 'url' | 'headers' | 'enabled' | 'environment' | 'autonomousToken'>;
5
+ payload?: Pick<McpServerProps, 'name' | 'url' | 'headers' | 'enabled' | 'environment'>;
6
6
  }
7
7
  export type Output = McpServerProps;
8
8
  export declare class CreateInternalMcpUseCase extends BaseUseCase<Params, Output> {
@@ -38,8 +38,8 @@ class CreateInternalMcpUseCase extends import_base.BaseUseCase {
38
38
  if (await this.mcpServerRepository.findInternalByName(payload.name, scope)) {
39
39
  this.throwHttpError({ statusCode: 409, errorMessage: `There is already an internal MCP named "${payload.name}"` });
40
40
  }
41
- const { name, url, headers, enabled, environment, autonomousToken } = payload;
42
- const mcpServer = import_entities.McpServer.fromJSON({ name, url, headers, enabled, environment, autonomousToken, type: "http" }).initializeInternal();
41
+ const { name, url, headers, enabled, environment } = payload;
42
+ const mcpServer = import_entities.McpServer.fromJSON({ name, url, headers, enabled, environment, type: "http" }).initializeInternal();
43
43
  await this.mcpServerRepository.create(mcpServer, scope);
44
44
  return mcpServer.toJSON();
45
45
  }
@@ -15,8 +15,8 @@ class CreateInternalMcpUseCase extends BaseUseCase {
15
15
  if (await this.mcpServerRepository.findInternalByName(payload.name, scope)) {
16
16
  this.throwHttpError({ statusCode: 409, errorMessage: `There is already an internal MCP named "${payload.name}"` });
17
17
  }
18
- const { name, url, headers, enabled, environment, autonomousToken } = payload;
19
- const mcpServer = McpServer.fromJSON({ name, url, headers, enabled, environment, autonomousToken, type: "http" }).initializeInternal();
18
+ const { name, url, headers, enabled, environment } = payload;
19
+ const mcpServer = McpServer.fromJSON({ name, url, headers, enabled, environment, type: "http" }).initializeInternal();
20
20
  await this.mcpServerRepository.create(mcpServer, scope);
21
21
  return mcpServer.toJSON();
22
22
  }
@@ -3,7 +3,7 @@ import { McpServerProps } from '../../entities';
3
3
  import { McpServerRepository } from '../../repositories';
4
4
  export interface Params extends BaseUseCaseParams {
5
5
  id: string;
6
- payload?: Pick<McpServerProps, 'name' | 'url' | 'headers' | 'enabled' | 'environment' | 'autonomousToken' | '__v'>;
6
+ payload?: Pick<McpServerProps, 'name' | 'url' | 'headers' | 'enabled' | 'environment' | '__v'>;
7
7
  }
8
8
  export type Output = McpServerProps;
9
9
  export declare class UpdateInternalMcpUseCase extends BaseUseCase<Params, Output> {
@@ -46,8 +46,8 @@ class UpdateInternalMcpUseCase extends import_base.BaseUseCase {
46
46
  }
47
47
  }
48
48
  if (payload.url !== void 0 && !payload.url) this.throwHttpError({ statusCode: 400, errorMessage: "An internal MCP needs a name and a url" });
49
- const { name, url, headers, enabled, environment, autonomousToken } = payload;
50
- const mcpServer = import_entities.McpServer.fromJSON({ ...existing, ...name !== void 0 && { name }, ...url !== void 0 && { url }, ...headers !== void 0 && { headers }, ...enabled !== void 0 && { enabled }, ...environment !== void 0 && { environment }, ...autonomousToken !== void 0 && { autonomousToken }, _id: id, internal: true });
49
+ const { name, url, headers, enabled, environment } = payload;
50
+ const mcpServer = import_entities.McpServer.fromJSON({ ...existing, ...name !== void 0 && { name }, ...url !== void 0 && { url }, ...headers !== void 0 && { headers }, ...enabled !== void 0 && { enabled }, ...environment !== void 0 && { environment }, _id: id, internal: true });
51
51
  const result = await this.mcpServerRepository.update(mcpServer, scope, { expectedVersion: existing.__v });
52
52
  if (!result) this.throwHttpError({ statusCode: 404, errorMessage: "{{resource_not_found}}" });
53
53
  return result;
@@ -23,8 +23,8 @@ class UpdateInternalMcpUseCase extends BaseUseCase {
23
23
  }
24
24
  }
25
25
  if (payload.url !== void 0 && !payload.url) this.throwHttpError({ statusCode: 400, errorMessage: "An internal MCP needs a name and a url" });
26
- const { name, url, headers, enabled, environment, autonomousToken } = payload;
27
- const mcpServer = McpServer.fromJSON({ ...existing, ...name !== void 0 && { name }, ...url !== void 0 && { url }, ...headers !== void 0 && { headers }, ...enabled !== void 0 && { enabled }, ...environment !== void 0 && { environment }, ...autonomousToken !== void 0 && { autonomousToken }, _id: id, internal: true });
26
+ const { name, url, headers, enabled, environment } = payload;
27
+ const mcpServer = McpServer.fromJSON({ ...existing, ...name !== void 0 && { name }, ...url !== void 0 && { url }, ...headers !== void 0 && { headers }, ...enabled !== void 0 && { enabled }, ...environment !== void 0 && { environment }, _id: id, internal: true });
28
28
  const result = await this.mcpServerRepository.update(mcpServer, scope, { expectedVersion: existing.__v });
29
29
  if (!result) this.throwHttpError({ statusCode: 404, errorMessage: "{{resource_not_found}}" });
30
30
  return result;
@@ -17,7 +17,6 @@ export interface McpServerProps extends BaseEntityProps {
17
17
  internal?: boolean;
18
18
  enabled?: boolean;
19
19
  environment?: string;
20
- autonomousToken?: string;
21
20
  }
22
21
  export declare class McpServer extends BaseEntity {
23
22
  private readonly props;
@@ -33,10 +32,9 @@ export declare class McpServer extends BaseEntity {
33
32
  internal?: boolean | undefined;
34
33
  enabled?: boolean | undefined;
35
34
  environment?: string | undefined;
36
- autonomousToken?: string | undefined;
37
35
  mcpClient?: Client;
38
36
  static fromJSON(props: McpServerProps): McpServer;
39
- constructor(props: McpServerProps, name?: string | undefined, url?: string | undefined, type?: McpType | undefined, headers?: Record<string, string> | undefined, trust?: McpTrust | undefined, approvedHash?: string | null | undefined, history?: McpManifest[] | undefined, status?: McpStatus | undefined, lastError?: string | null | undefined, internal?: boolean | undefined, enabled?: boolean | undefined, environment?: string | undefined, autonomousToken?: string | undefined);
37
+ constructor(props: McpServerProps, name?: string | undefined, url?: string | undefined, type?: McpType | undefined, headers?: Record<string, string> | undefined, trust?: McpTrust | undefined, approvedHash?: string | null | undefined, history?: McpManifest[] | undefined, status?: McpStatus | undefined, lastError?: string | null | undefined, internal?: boolean | undefined, enabled?: boolean | undefined, environment?: string | undefined);
40
38
  toJSON(): McpServerProps;
41
39
  get latest(): McpManifest | undefined;
42
40
  get approved(): McpManifest | undefined;
@@ -30,7 +30,7 @@ __reExport(mcpserver_entity_exports, require("./mcp-manifest"), module.exports);
30
30
  const MAX_HISTORY = 5;
31
31
  const MAX_ERROR_CHARS = 300;
32
32
  class McpServer extends import_base.BaseEntity {
33
- constructor(props, name, url, type, headers, trust, approvedHash, history, status, lastError, internal, enabled, environment, autonomousToken) {
33
+ constructor(props, name, url, type, headers, trust, approvedHash, history, status, lastError, internal, enabled, environment) {
34
34
  super(props);
35
35
  this.props = props;
36
36
  this.name = name;
@@ -45,7 +45,6 @@ class McpServer extends import_base.BaseEntity {
45
45
  this.internal = internal;
46
46
  this.enabled = enabled;
47
47
  this.environment = environment;
48
- this.autonomousToken = autonomousToken;
49
48
  }
50
49
  props;
51
50
  name;
@@ -60,7 +59,6 @@ class McpServer extends import_base.BaseEntity {
60
59
  internal;
61
60
  enabled;
62
61
  environment;
63
- autonomousToken;
64
62
  mcpClient;
65
63
  static fromJSON(props) {
66
64
  return new McpServer(
@@ -76,8 +74,7 @@ class McpServer extends import_base.BaseEntity {
76
74
  props.lastError,
77
75
  props.internal,
78
76
  props.enabled,
79
- props.environment,
80
- props.autonomousToken
77
+ props.environment
81
78
  );
82
79
  }
83
80
  toJSON() {
@@ -94,8 +91,7 @@ class McpServer extends import_base.BaseEntity {
94
91
  lastError: this.lastError,
95
92
  internal: this.internal,
96
93
  enabled: this.enabled,
97
- environment: this.environment,
98
- autonomousToken: this.autonomousToken
94
+ environment: this.environment
99
95
  };
100
96
  }
101
97
  get latest() {
@@ -115,7 +111,6 @@ class McpServer extends import_base.BaseEntity {
115
111
  this.lastError = null;
116
112
  this.internal = void 0;
117
113
  this.enabled = void 0;
118
- this.autonomousToken = void 0;
119
114
  return this;
120
115
  }
121
116
  initializeInternal() {
@@ -6,7 +6,7 @@ export * from "./mcp-manifest";
6
6
  const MAX_HISTORY = 5;
7
7
  const MAX_ERROR_CHARS = 300;
8
8
  class McpServer extends BaseEntity {
9
- constructor(props, name, url, type, headers, trust, approvedHash, history, status, lastError, internal, enabled, environment, autonomousToken) {
9
+ constructor(props, name, url, type, headers, trust, approvedHash, history, status, lastError, internal, enabled, environment) {
10
10
  super(props);
11
11
  this.props = props;
12
12
  this.name = name;
@@ -21,7 +21,6 @@ class McpServer extends BaseEntity {
21
21
  this.internal = internal;
22
22
  this.enabled = enabled;
23
23
  this.environment = environment;
24
- this.autonomousToken = autonomousToken;
25
24
  }
26
25
  props;
27
26
  name;
@@ -36,7 +35,6 @@ class McpServer extends BaseEntity {
36
35
  internal;
37
36
  enabled;
38
37
  environment;
39
- autonomousToken;
40
38
  mcpClient;
41
39
  static fromJSON(props) {
42
40
  return new McpServer(
@@ -52,8 +50,7 @@ class McpServer extends BaseEntity {
52
50
  props.lastError,
53
51
  props.internal,
54
52
  props.enabled,
55
- props.environment,
56
- props.autonomousToken
53
+ props.environment
57
54
  );
58
55
  }
59
56
  toJSON() {
@@ -70,8 +67,7 @@ class McpServer extends BaseEntity {
70
67
  lastError: this.lastError,
71
68
  internal: this.internal,
72
69
  enabled: this.enabled,
73
- environment: this.environment,
74
- autonomousToken: this.autonomousToken
70
+ environment: this.environment
75
71
  };
76
72
  }
77
73
  get latest() {
@@ -91,7 +87,6 @@ class McpServer extends BaseEntity {
91
87
  this.lastError = null;
92
88
  this.internal = void 0;
93
89
  this.enabled = void 0;
94
- this.autonomousToken = void 0;
95
90
  return this;
96
91
  }
97
92
  initializeInternal() {
@@ -2,6 +2,5 @@ export interface StoredInternalMcp {
2
2
  name: string;
3
3
  url: string;
4
4
  headers?: Record<string, string>;
5
- autonomousToken?: string;
6
5
  }
7
6
  export declare function listActiveInternalMcps(): Promise<StoredInternalMcp[]>;
@@ -24,7 +24,7 @@ module.exports = __toCommonJS(internal_exports);
24
24
  var import_repositories = require("./repositories");
25
25
  async function listActiveInternalMcps() {
26
26
  const items = await new import_repositories.McpServerRepository().findActiveInternals();
27
- return items.flatMap((item) => item.name && item.url ? [{ name: item.name, url: item.url, ...item.headers ? { headers: item.headers } : {}, ...item.autonomousToken ? { autonomousToken: item.autonomousToken } : {} }] : []);
27
+ return items.flatMap((item) => item.name && item.url ? [{ name: item.name, url: item.url, ...item.headers ? { headers: item.headers } : {} }] : []);
28
28
  }
29
29
  // Annotate the CommonJS export names for ESM import in node:
30
30
  0 && (module.exports = {
@@ -1,7 +1,7 @@
1
1
  import { McpServerRepository } from "./repositories";
2
2
  async function listActiveInternalMcps() {
3
3
  const items = await new McpServerRepository().findActiveInternals();
4
- return items.flatMap((item) => item.name && item.url ? [{ name: item.name, url: item.url, ...item.headers ? { headers: item.headers } : {}, ...item.autonomousToken ? { autonomousToken: item.autonomousToken } : {} }] : []);
4
+ return items.flatMap((item) => item.name && item.url ? [{ name: item.name, url: item.url, ...item.headers ? { headers: item.headers } : {} }] : []);
5
5
  }
6
6
  export {
7
7
  listActiveInternalMcps
@@ -48,7 +48,7 @@ class UpdateMcpServerUseCase extends import_base.BaseUseCase {
48
48
  }
49
49
  const endpointChanged = payload.url !== void 0 && payload.url !== existing.url || payload.type !== void 0 && payload.type !== existing.type;
50
50
  const headersChanged = payload.headers !== void 0 && JSON.stringify(payload.headers) !== JSON.stringify(existing.headers);
51
- const mcpServer = import_entities.McpServer.fromJSON({ ...existing, ...payload, _id: id, trust: existing.trust, approvedHash: existing.approvedHash, status: existing.status, lastError: existing.lastError, history: existing.history, internal: existing.internal, enabled: existing.enabled, autonomousToken: existing.autonomousToken });
51
+ const mcpServer = import_entities.McpServer.fromJSON({ ...existing, ...payload, _id: id, trust: existing.trust, approvedHash: existing.approvedHash, status: existing.status, lastError: existing.lastError, history: existing.history, internal: existing.internal, enabled: existing.enabled });
52
52
  if (endpointChanged) {
53
53
  mcpServer.lockForNewEndpoint();
54
54
  }
@@ -25,7 +25,7 @@ class UpdateMcpServerUseCase extends BaseUseCase {
25
25
  }
26
26
  const endpointChanged = payload.url !== void 0 && payload.url !== existing.url || payload.type !== void 0 && payload.type !== existing.type;
27
27
  const headersChanged = payload.headers !== void 0 && JSON.stringify(payload.headers) !== JSON.stringify(existing.headers);
28
- const mcpServer = McpServer.fromJSON({ ...existing, ...payload, _id: id, trust: existing.trust, approvedHash: existing.approvedHash, status: existing.status, lastError: existing.lastError, history: existing.history, internal: existing.internal, enabled: existing.enabled, autonomousToken: existing.autonomousToken });
28
+ const mcpServer = McpServer.fromJSON({ ...existing, ...payload, _id: id, trust: existing.trust, approvedHash: existing.approvedHash, status: existing.status, lastError: existing.lastError, history: existing.history, internal: existing.internal, enabled: existing.enabled });
29
29
  if (endpointChanged) {
30
30
  mcpServer.lockForNewEndpoint();
31
31
  }
@@ -343,6 +343,7 @@ const runner = createAutomationRunner({
343
343
  - The header is added only in autonomous runs and only to internal servers that use `{{token}}`. It replaces any `X-User-Id` you configured. It is never sent to the servers added by the user.
344
344
  - An interactive run never uses the `autonomousToken`. A server without one is unavailable in the automations that need a token, and the run continues without it.
345
345
  - Each server has its own token, so two servers can use different service accounts. A server that must use a service account in every run, also in chats, does not need this: write the token in the header (for example `Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJjcm0uZXhhbXBsZS5jb20iLCJzdWIiOiJzdmMtb3Blbmxvb3BzLWNybSIsImF1ZCI6ImNybS1tY3AiLCJzY29wZSI6ImNybTpyZWFkIGNybTp3cml0ZSIsImlhdCI6MTc2MjIxNDQwMCwiZXhwIjoxNzkzNzUwNDAwfQ.YCtd8mv7RZpaZb3Z1aSl25MGP2QRWHc8RtA82MJOH7U`).
346
+ - `autonomousToken` exists only in the servers declared in code. A server stored with the back office is [available in an automation](mcp-servers.md#internal-servers-stored-in-the-database) only if its header has the token written in it.
346
347
  - This field is temporary. It will go away when the integrations let the runner get the token of the owner.
347
348
 
348
349
  ### Running by hand
@@ -247,7 +247,6 @@ const mcp = await createInternalMcp({
247
247
  name: 'budget',
248
248
  url: 'https://api.example.com/mcp',
249
249
  headers: { Authorization: 'Bearer {{token}}' },
250
- autonomousToken: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJjcm0uZXhhbXBsZS5jb20iLCJzdWIiOiJzdmMtb3Blbmxvb3BzLWNybSIsImF1ZCI6ImNybS1tY3AiLCJzY29wZSI6ImNybTpyZWFkIGNybTp3cml0ZSIsImlhdCI6MTc2MjIxNDQwMCwiZXhwIjoxNzkzNzUwNDAwfQ.YCtd8mv7RZpaZb3Z1aSl25MGP2QRWHc8RtA82MJOH7U',
251
250
  },
252
251
  currentUser: admin,
253
252
  })
@@ -256,12 +255,12 @@ await updateInternalMcp({ id: mcp._id, payload: { enabled: false, __v: mcp.__v }
256
255
  ```
257
256
 
258
257
  - Every function needs an admin as `currentUser` and answers 403 otherwise. The sub-route separates the code, the permission is what protects it.
259
- - `name` and `url` are required, `type` is always `http`, `environment` and `autonomousToken` are optional and `enabled` defaults to `true`. A disabled server is not loaded. The name must be unique among the internal servers (409).
260
- - A header with `{{token}}` works as in the declared ones: it uses `mcpTokens[name]` of the run, or its `autonomousToken` in an autonomous run. A header with the token written in it (for example `Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJjcm0uZXhhbXBsZS5jb20iLCJzdWIiOiJzdmMtb3Blbmxvb3BzLWNybSIsImF1ZCI6ImNybS1tY3AiLCJzY29wZSI6ImNybTpyZWFkIGNybTp3cml0ZSIsImlhdCI6MTc2MjIxNDQwMCwiZXhwIjoxNzkzNzUwNDAwfQ.YCtd8mv7RZpaZb3Z1aSl25MGP2QRWHc8RtA82MJOH7U`) is sent as it is and needs nothing from the run.
258
+ - `name` and `url` are required, `type` is always `http`, `environment` is optional and `enabled` defaults to `true`. A disabled server is not loaded. The name must be unique among the internal servers (409).
259
+ - A header with `{{token}}` uses `mcpTokens[name]` of the run. The stored servers have no `autonomousToken`, which exists only in the servers declared in code, so in an automation they are available only when the token is written in the header (for example `Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJjcm0uZXhhbXBsZS5jb20iLCJzdWIiOiJzdmMtb3Blbmxvb3BzLWNybSIsImF1ZCI6ImNybS1tY3AiLCJzY29wZSI6ImNybTpyZWFkIGNybTp3cml0ZSIsImlhdCI6MTc2MjIxNDQwMCwiZXhwIjoxNzkzNzUwNDAwfQ.YCtd8mv7RZpaZb3Z1aSl25MGP2QRWHc8RtA82MJOH7U`), which is sent as it is in every run.
261
260
  - Deleting is permanent. Internal servers have no owner, so they are not listed to any user.
262
261
  - The normal functions (`getMcpServerById`, `updateMcpServer`, `deleteMcpServer`, `decideMcpServerTrust`, and the lists) never see them, not even for an admin, and `internal` and `enabled` in the payload of a user are ignored.
263
262
  - If a server declared in code has the same name, the one in code wins. A server of a user with the same name as an internal one is ignored for that run.
264
- - `headers` and `autonomousToken` are stored as written, like the headers of the servers of the users. Mask them in your controller before sending a record to a client.
263
+ - `headers` are stored as written, like the headers of the servers of the users. Mask them in your controller before sending a record to a client.
265
264
  - Run `ensureIndexes` after updating: it adds the unique index of the names of the internal servers.
266
265
 
267
266
  ## Tools of MCP servers
@@ -1,6 +1,11 @@
1
1
  import { Logger } from 'openloops/common'
2
2
  import { login } from 'openloops/authentication'
3
3
 
4
+ /**
5
+ * Logs in with an email or a username and a password.
6
+ *
7
+ * The result has a short access token, a refresh token and the user. Send the access token to your backend on every request and use the refresh token to get a new one when it expires.
8
+ */
4
9
  async function loginExample () {
5
10
  const logger = new Logger()
6
11
 
@@ -2,6 +2,11 @@ import { Logger } from 'openloops/common'
2
2
  import { CurrentUser } from 'openloops/base'
3
3
  import { createAutomation } from 'openloops/automations'
4
4
 
5
+ /**
6
+ * Creates an automation that runs on a schedule: a weekly report at 08:00, Monday to Friday, in the timezone of the schedule.
7
+ *
8
+ * The run uses the model that you set here. The next run is calculated and returned in `triggers.schedule.nextRunAt`.
9
+ */
5
10
  async function createSchedule () {
6
11
  const logger = new Logger()
7
12
  const currentUser = await CurrentUser.asyncFromDB('6ab56894eb20887d1ef7942c')
@@ -2,6 +2,11 @@ import { Logger } from 'openloops/common'
2
2
  import { CurrentUser } from 'openloops/base'
3
3
  import { createAutomation, issueAutomationEventToken } from 'openloops/automations'
4
4
 
5
+ /**
6
+ * Creates an automation that starts when another system sends it an event, and issues the token that system must use.
7
+ *
8
+ * The token is not stored: the owner can issue as many as they want. It expires in 90 days here, and without `expiresIn` it never expires.
9
+ */
5
10
  async function createEventAutomation () {
6
11
  const logger = new Logger()
7
12
  const currentUser = await CurrentUser.asyncFromDB('6ab56894eb20887d1ef7942c')
@@ -2,6 +2,9 @@ import { Logger } from 'openloops/common'
2
2
  import { CurrentUser } from 'openloops/base'
3
3
  import { listAutomationRunsByAutomation, listAutomationRunsByUser } from 'openloops/automations'
4
4
 
5
+ /**
6
+ * Lists the history of runs of one automation, with its status and the chat where each run happened.
7
+ */
5
8
  async function listRunsOfAnAutomation () {
6
9
  const logger = new Logger()
7
10
  const currentUser = await CurrentUser.asyncFromDB('6ab56894eb20887d1ef7942c')
@@ -15,6 +18,9 @@ async function listRunsOfAnAutomation () {
15
18
  logger.info(result.items.map(run => ({ status: run.status, startedAt: run.startedAt, chatId: run.chatId, error: run.error })), '[Demo] Runs of the automation')
16
19
  }
17
20
 
21
+ /**
22
+ * Counts the runs of the user that are paused (waiting for an answer in the chat) or failed, to show them in a screen of pending items.
23
+ */
18
24
  async function listRunsThatNeedAttention () {
19
25
  const logger = new Logger()
20
26
  const currentUser = await CurrentUser.asyncFromDB('6ab56894eb20887d1ef7942c')
@@ -2,6 +2,9 @@ import { Logger } from 'openloops/common'
2
2
  import { CurrentUser } from 'openloops/base'
3
3
  import { listAutomationsByUser } from 'openloops/automations'
4
4
 
5
+ /**
6
+ * Lists the automations of the user with the state of their triggers and the result of the last run.
7
+ */
5
8
  async function listAutomations () {
6
9
  const logger = new Logger()
7
10
  const currentUser = await CurrentUser.asyncFromDB('6ab56894eb20887d1ef7942c')
@@ -1,6 +1,9 @@
1
1
  import { Logger } from 'openloops/common'
2
2
  import { previewAutomationRuns } from 'openloops/automations'
3
3
 
4
+ /**
5
+ * Shows the next dates a recurrence would run, without creating anything. Use it in a form so the user sees what they are scheduling (here, the last day of every month at 18:00).
6
+ */
4
7
  async function previewRuns () {
5
8
  const logger = new Logger()
6
9
 
@@ -1,6 +1,11 @@
1
1
  import { Logger } from 'openloops/common'
2
2
  import { AutomationEventError, receiveAutomationEvent } from 'openloops/automations'
3
3
 
4
+ /**
5
+ * Receives an event from another system, for example in the endpoint of your backend that gets the call of an alarm.
6
+ *
7
+ * The token travels in the header `Authorization: Bearer ...`. The `idempotencyKey` is optional: with it, a repeated event does not start a second run. A refused event throws an `AutomationEventError` that carries the HTTP status to answer.
8
+ */
4
9
  async function receive (automationId: string, authorization: string, body: unknown, idempotencyKey?: string) {
5
10
  const logger = new Logger()
6
11
 
@@ -2,6 +2,9 @@ import { Logger } from 'openloops/common'
2
2
  import { CurrentUser } from 'openloops/base'
3
3
  import { revokeAutomationEventToken, rotateAutomationEventTokens } from 'openloops/automations'
4
4
 
5
+ /**
6
+ * Revokes one token of an automation, for example one that leaked, by presenting it. The other tokens keep working.
7
+ */
5
8
  async function revokeLeakedToken () {
6
9
  const logger = new Logger()
7
10
  const currentUser = await CurrentUser.asyncFromDB('6ab56894eb20887d1ef7942c')
@@ -11,6 +14,9 @@ async function revokeLeakedToken () {
11
14
  logger.info({ jti: result.jti }, '[Demo] That token no longer works, the others still do')
12
15
  }
13
16
 
17
+ /**
18
+ * Invalidates every token issued for the automation at once. Issue a new one afterwards.
19
+ */
14
20
  async function rotateAll () {
15
21
  const logger = new Logger()
16
22
  const currentUser = await CurrentUser.asyncFromDB('6ab56894eb20887d1ef7942c')
@@ -3,6 +3,9 @@ import { CurrentUser } from 'openloops/base'
3
3
  import { createAutomationRunner } from 'openloops/automations'
4
4
  import { webSearchTool } from 'openloops/tools'
5
5
 
6
+ /**
7
+ * Starts a run of an automation by hand, outside its schedule, for example from a "run now" button. The run goes on in the background.
8
+ */
6
9
  async function runNow () {
7
10
  const logger = new Logger()
8
11
  const currentUser = await CurrentUser.asyncFromDB('6ab56894eb20887d1ef7942c')
@@ -8,6 +8,13 @@ async function notifyOwner (message: string) {
8
8
  logger.info({ message }, '[Notification Service] Sending notification to the owner...')
9
9
  }
10
10
 
11
+ /**
12
+ * Worker that runs the automations. Start it in its own process: it looks for due schedules and queued events and runs each one in autonomous mode.
13
+ *
14
+ * The internal MCP `crm` uses the token of the user when there is one. An automation has no session of the user, so there it uses `autonomousToken`, the token of a service account, and the agent also sends the id of the owner in `X-User-Id`.
15
+ *
16
+ * `onRunFinished` is where you notify the owner when a run fails, is paused or is skipped.
17
+ */
11
18
  const runner = createAutomationRunner({
12
19
  agent: {
13
20
  tools: [webSearchTool],
@@ -2,6 +2,9 @@ import { Logger } from 'openloops/common'
2
2
  import { CurrentUser } from 'openloops/base'
3
3
  import { updateAutomation } from 'openloops/automations'
4
4
 
5
+ /**
6
+ * Turns off the schedule of an automation without deleting it. Triggers are merged key by key, so only `enabled` changes.
7
+ */
5
8
  async function pause () {
6
9
  const logger = new Logger()
7
10
  const currentUser = await CurrentUser.asyncFromDB('6ab56894eb20887d1ef7942c')
@@ -15,6 +18,9 @@ async function pause () {
15
18
  logger.info({ enabled: result.triggers?.schedule?.enabled }, '[Demo] The automation is off')
16
19
  }
17
20
 
21
+ /**
22
+ * Turns the schedule on again and changes its recurrence at the same time. The next run is calculated again.
23
+ */
18
24
  async function resume () {
19
25
  const logger = new Logger()
20
26
  const currentUser = await CurrentUser.asyncFromDB('6ab56894eb20887d1ef7942c')
@@ -2,6 +2,9 @@ import { Logger } from 'openloops/common'
2
2
  import { CurrentUser } from 'openloops/base'
3
3
  import { deleteChat } from 'openloops/chats'
4
4
 
5
+ /**
6
+ * Deletes a chat of the user without removing it from the database (soft delete).
7
+ */
5
8
  async function softDeleteChat () {
6
9
  const logger = new Logger()
7
10
  const currentUser = await CurrentUser.asyncFromDB('6ab56894eb20887d1ef7942c')
@@ -14,6 +17,9 @@ async function softDeleteChat () {
14
17
  logger.info(result, '[Demo] Deleted chat')
15
18
  }
16
19
 
20
+ /**
21
+ * Removes a chat from the database for good. Only an admin can do it.
22
+ */
17
23
  async function hardDeleteChat () {
18
24
  const logger = new Logger()
19
25
  const currentUser = await CurrentUser.asyncFromDB('6ab56894eb20887d1ef7942c')
@@ -2,6 +2,9 @@ import { Logger } from 'openloops/common'
2
2
  import { CurrentUser } from 'openloops/base'
3
3
  import { getChatById } from 'openloops/chats'
4
4
 
5
+ /**
6
+ * Gets a chat with its messages.
7
+ */
5
8
  async function getChat () {
6
9
  const logger = new Logger()
7
10
  const currentUser = await CurrentUser.asyncFromDB('6ab56894eb20887d1ef7942c')
@@ -14,6 +17,9 @@ async function getChat () {
14
17
  logger.info(result?.messages, '[Demo] Messages')
15
18
  }
16
19
 
20
+ /**
21
+ * Gets only the state of a chat, with `select`, which is lighter. Use it to know if a run is still going or is waiting for the user.
22
+ */
17
23
  async function getChatStatus () {
18
24
  const logger = new Logger()
19
25
  const currentUser = await CurrentUser.asyncFromDB('6ab56894eb20887d1ef7942c')
@@ -2,6 +2,9 @@ import { Logger } from 'openloops/common'
2
2
  import { CurrentUser } from 'openloops/base'
3
3
  import { listChatsByUser } from 'openloops/chats'
4
4
 
5
+ /**
6
+ * Lists the chats created by autonomous runs, and keeps the ones that need the user: waiting for a confirmation, waiting for an answer, or failed.
7
+ */
5
8
  async function listAutonomousChats () {
6
9
  const logger = new Logger()
7
10
  const currentUser = await CurrentUser.asyncFromDB('6ab56894eb20887d1ef7942c')
@@ -2,6 +2,9 @@ import { Logger } from 'openloops/common'
2
2
  import { CurrentUser } from 'openloops/base'
3
3
  import { listChatsByUser } from 'openloops/chats'
4
4
 
5
+ /**
6
+ * Lists the chats of the user in pages.
7
+ */
5
8
  async function listChats () {
6
9
  const logger = new Logger()
7
10
  const currentUser = await CurrentUser.asyncFromDB('6ab56894eb20887d1ef7942c')
@@ -1,5 +1,10 @@
1
1
  import { Guard } from 'openloops/guardrails'
2
2
 
3
+ /**
4
+ * An input guard of your own. The handler receives the message before the loop runs and returns `{ passed: true }` or `{ passed: false, reason }`. When it blocks, the user gets `reply` and the model never sees the message.
5
+ *
6
+ * Add it with `guards: [noSpam]` in the `Agent` or with `agent.addGuard(noSpam)`.
7
+ */
3
8
  export const noSpam = new Guard({
4
9
  name: 'no_spam',
5
10
  description: 'Blocks obvious spam phrases',
@@ -3,6 +3,11 @@ import { Logger } from 'openloops/common'
3
3
  import { CurrentUser } from 'openloops/base'
4
4
  import { fastResponder } from 'openloops/loops'
5
5
 
6
+ /**
7
+ * A loop of your own. It has one node, `respond`, that answers with the fast responder and ends the turn. Add more nodes to build a flow with steps.
8
+ *
9
+ * Change the `version` every time the loop or its prompts change, it is saved with every turn.
10
+ */
6
11
  export class EchoLoop extends AgentLoop {
7
12
  get initialNode () {
8
13
  return this.nodes.respond
@@ -35,6 +40,9 @@ export class EchoLoop extends AgentLoop {
35
40
  }
36
41
  }
37
42
 
43
+ /**
44
+ * Runs an agent with the loop above.
45
+ */
38
46
  async function runCustomLoop () {
39
47
  const logger = new Logger()
40
48
  const currentUser = await CurrentUser.asyncFromDB('6ab56894eb20887d1ef7942c')
@@ -2,6 +2,11 @@ import { Logger } from 'openloops/common'
2
2
  import { CurrentUser } from 'openloops/base'
3
3
  import { createMcpServer } from 'openloops/mcps'
4
4
 
5
+ /**
6
+ * Registers an MCP server of the user. It starts `locked`: the first time the agent needs it, the user must approve it.
7
+ *
8
+ * `environment` is only a label (for example `staging`), the agent does not use it. Creating the server connects once to read its manifest.
9
+ */
5
10
  async function createMcp () {
6
11
  const logger = new Logger()
7
12
  const currentUser = await CurrentUser.asyncFromDB('6ab56894eb20887d1ef7942c')
@@ -2,6 +2,9 @@ import { Logger } from 'openloops/common'
2
2
  import { CurrentUser } from 'openloops/base'
3
3
  import { getMcpServerById, decideMcpServerTrust } from 'openloops/mcps'
4
4
 
5
+ /**
6
+ * Approves a server with the hash of the manifest that the user reviewed. If the server changed since then, the call fails and the user has to review it again.
7
+ */
5
8
  async function approveMcp () {
6
9
  const logger = new Logger()
7
10
  const currentUser = await CurrentUser.asyncFromDB('6ab56894eb20887d1ef7942c')
@@ -22,6 +25,9 @@ async function approveMcp () {
22
25
  logger.info({ trust: result.trust, approvedHash: result.approvedHash }, '[Demo] Approved mcp')
23
26
  }
24
27
 
28
+ /**
29
+ * Blocks a server. The agent will not list or call its tools until it is approved again.
30
+ */
25
31
  async function blockMcp () {
26
32
  const logger = new Logger()
27
33
  const currentUser = await CurrentUser.asyncFromDB('6ab56894eb20887d1ef7942c')
@@ -2,6 +2,9 @@ import { Logger } from 'openloops/common'
2
2
  import { CurrentUser } from 'openloops/base'
3
3
  import { deleteMcpServer } from 'openloops/mcps'
4
4
 
5
+ /**
6
+ * Deletes a server of the user without removing it from the database.
7
+ */
5
8
  async function softDeleteMcp () {
6
9
  const logger = new Logger()
7
10
  const currentUser = await CurrentUser.asyncFromDB('6ab56894eb20887d1ef7942c')
@@ -14,6 +17,9 @@ async function softDeleteMcp () {
14
17
  logger.info(result, '[Demo] Deleted mcp result')
15
18
  }
16
19
 
20
+ /**
21
+ * Removes a server from the database for good. Only an admin can do it.
22
+ */
17
23
  async function hardDeleteMcp () {
18
24
  const logger = new Logger()
19
25
  const currentUser = await CurrentUser.asyncFromDB('6ab56894eb20887d1ef7942c')
@@ -2,6 +2,9 @@ import { Logger } from 'openloops/common'
2
2
  import { CurrentUser } from 'openloops/base'
3
3
  import { getMcpServerById } from 'openloops/mcps'
4
4
 
5
+ /**
6
+ * Gets one MCP server of the user, with its manifest history.
7
+ */
5
8
  async function getMcpById () {
6
9
  const logger = new Logger()
7
10
  const currentUser = await CurrentUser.asyncFromDB('6ab56894eb20887d1ef7942c')
@@ -2,25 +2,39 @@ import { Logger } from 'openloops/common'
2
2
  import { CurrentUser } from 'openloops/base'
3
3
  import { createInternalMcp, updateInternalMcp, listInternalMcps, deleteInternalMcp } from 'openloops/mcps/admin'
4
4
 
5
+ /**
6
+ * Back office of the internal MCP servers. It needs an admin, anyone else gets 403.
7
+ *
8
+ * These servers are stored in the database, trusted (no manifest review) and loaded by the agent in every run. The users never see them.
9
+ */
5
10
  async function manageInternalMcps () {
6
11
  const logger = new Logger()
7
12
  const admin = await CurrentUser.asyncFromDB('6ab56894eb20887d1ef7942c')
8
13
 
9
14
  if (!admin) throw new Error('[Demo] admin is not defined')
10
15
 
16
+ /**
17
+ * An MCP with a template. `{{token}}` is replaced in each run with the token of the user, passed in `mcpTokens`, so the server acts on behalf of that user.
18
+ *
19
+ * If a run brings no token for it, the server is unavailable for that run and the agent continues without it. In an automation there is no token of the user, so it is unavailable there too.
20
+ */
11
21
  const crm = await createInternalMcp({
12
22
  payload: {
13
23
  name: 'crm',
14
24
  url: 'https://crm.example.com/mcp',
15
25
  environment: 'production',
16
26
  headers: { Authorization: 'Bearer {{token}}' },
17
- autonomousToken: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJjcm0uZXhhbXBsZS5jb20iLCJzdWIiOiJzdmMtb3Blbmxvb3BzLWNybSIsImF1ZCI6ImNybS1tY3AiLCJzY29wZSI6ImNybTpyZWFkIGNybTp3cml0ZSIsImlhdCI6MTc2MjIxNDQwMCwiZXhwIjoxNzkzNzUwNDAwfQ.YCtd8mv7RZpaZb3Z1aSl25MGP2QRWHc8RtA82MJOH7U',
18
27
  },
19
28
  currentUser: admin,
20
29
  })
21
30
 
22
31
  logger.info(crm, '[Demo] Created internal mcp:')
23
32
 
33
+ /**
34
+ * An MCP with a fixed token. The header has the token written in it, so it is sent as it is in every run, for every user and also in automations.
35
+ *
36
+ * Use it only for a service account that does not depend on the user. The token is stored as written, so do not return it to a client.
37
+ */
24
38
  const catalog = await createInternalMcp({
25
39
  payload: {
26
40
  name: 'catalog',
@@ -31,12 +45,21 @@ async function manageInternalMcps () {
31
45
  currentUser: admin,
32
46
  })
33
47
 
48
+ /**
49
+ * Disables the server without deleting it. A disabled server is not loaded by the agent.
50
+ */
34
51
  await updateInternalMcp({ id: String(catalog._id), payload: { enabled: false }, currentUser: admin })
35
52
 
53
+ /**
54
+ * Lists the internal servers in pages.
55
+ */
36
56
  const page = await listInternalMcps({ options: { page: 1, limit: 10 }, currentUser: admin })
37
57
 
38
58
  logger.info({ names: page.items.map(item => item.name) }, '[Demo] Internal mcps:')
39
59
 
60
+ /**
61
+ * Deletes the server for good.
62
+ */
40
63
  await deleteInternalMcp({ id: String(catalog._id), currentUser: admin })
41
64
  }
42
65
 
@@ -3,6 +3,11 @@ import { Logger } from 'openloops/common'
3
3
  import { CurrentUser } from 'openloops/base'
4
4
  import { AgiLoop } from 'openloops/loops'
5
5
 
6
+ /**
7
+ * Declares internal MCP servers in code. They belong to your solution, not to the user, and they are trusted: no approval, no manifest review, no notice of third party. A destructive tool still asks the user.
8
+ *
9
+ * `mcpTokens` gives the token of the user to the servers that use `{{token}}`, by name. It is never saved.
10
+ */
6
11
  async function runAgentWithInternalMcp (sessionToken: string) {
7
12
  const logger = new Logger()
8
13
  const currentUser = await CurrentUser.asyncFromDB('6ab56894eb20887d1ef7942c')
@@ -12,17 +17,26 @@ async function runAgentWithInternalMcp (sessionToken: string) {
12
17
  const agent = new Agent({
13
18
  loop: new AgiLoop(),
14
19
  internalMcps: [
20
+ /**
21
+ * An MCP with a template: it uses the token of the user from `mcpTokens`. In an automation there is no token, so it is unavailable there.
22
+ */
15
23
  {
16
24
  name: 'budget',
17
25
  url: 'https://budget.example.com/mcp',
18
26
  headers: { Authorization: 'Bearer {{token}}' },
19
27
  },
28
+ /**
29
+ * An MCP with a template and a token for automations: in a chat it uses the token of the user. In an autonomous run, such as an automation, that brings no token for it, it uses `autonomousToken`, the token of a service account, and the agent sends the id of the owner in `X-User-Id`. A chat never uses `autonomousToken`.
30
+ */
20
31
  {
21
32
  name: 'crm',
22
33
  url: 'https://crm.example.com/mcp',
23
34
  headers: { Authorization: 'Bearer {{token}}' },
24
35
  autonomousToken: process.env.CRM_SERVICE_TOKEN,
25
36
  },
37
+ /**
38
+ * An MCP with a fixed token: always the service account, in chats and in automations, for every user. Use it only for data that does not depend on the user.
39
+ */
26
40
  {
27
41
  name: 'catalog',
28
42
  url: 'https://catalog.example.com/mcp',
@@ -2,6 +2,9 @@ import { Logger } from 'openloops/common'
2
2
  import { CurrentUser } from 'openloops/base'
3
3
  import { listAllMcpServersByUser } from 'openloops/mcps'
4
4
 
5
+ /**
6
+ * Lists every MCP server of the user, oldest first, without pages. It is what the agent uses. The internal servers never appear here.
7
+ */
5
8
  async function listAllMcps () {
6
9
  const logger = new Logger()
7
10
  const currentUser = await CurrentUser.asyncFromDB('6ab56894eb20887d1ef7942c')
@@ -2,6 +2,9 @@ import { Logger } from 'openloops/common'
2
2
  import { CurrentUser } from 'openloops/base'
3
3
  import { listMcpServersByUser } from 'openloops/mcps'
4
4
 
5
+ /**
6
+ * Lists the MCP servers of the user in pages, for a management screen. The internal servers never appear here.
7
+ */
5
8
  async function getMcpById () {
6
9
  const logger = new Logger()
7
10
  const currentUser = await CurrentUser.asyncFromDB('6ab56894eb20887d1ef7942c')
@@ -2,6 +2,11 @@ import { Logger } from 'openloops/common'
2
2
  import { CurrentUser } from 'openloops/base'
3
3
  import { updateMcpServer } from 'openloops/mcps'
4
4
 
5
+ /**
6
+ * Updates a server of the user. Here the header carries a fixed token: the user wrote it, and it is sent as it is.
7
+ *
8
+ * Changing the headers checks the connection again and keeps the approval. Changing the `url` locks the server again.
9
+ */
5
10
  async function updateMcp () {
6
11
  const logger = new Logger()
7
12
  const currentUser = await CurrentUser.asyncFromDB('6ab56894eb20887d1ef7942c')
@@ -2,6 +2,9 @@ import { Logger } from 'openloops/common'
2
2
  import { CurrentUser } from 'openloops/base'
3
3
  import { verifyMcpServerConnection } from 'openloops/mcps'
4
4
 
5
+ /**
6
+ * Checks the connection of a server: it reads the manifest and saves the status (`ok` or `error`). Use it in a "test connection" button, it also feeds a green or red dot.
7
+ */
5
8
  async function verifyMcp () {
6
9
  const logger = new Logger()
7
10
  const currentUser = await CurrentUser.asyncFromDB('6ab56894eb20887d1ef7942c')
@@ -2,6 +2,9 @@ import { Agent } from 'openloops/core'
2
2
  import { Logger } from 'openloops/common'
3
3
  import { CurrentUser } from 'openloops/base'
4
4
 
5
+ /**
6
+ * Asks a running chat to stop. The loop stops before its next node, so a node that is already running finishes first.
7
+ */
5
8
  async function interruptAgent () {
6
9
  const logger = new Logger()
7
10
  const currentUser = await CurrentUser.asyncFromDB('6ab56894eb20887d1ef7942c')
@@ -3,6 +3,9 @@ import { Logger } from 'openloops/common'
3
3
  import { CurrentUser } from 'openloops/base'
4
4
  import { AgiLoop } from 'openloops/loops'
5
5
 
6
+ /**
7
+ * Runs the agent with nobody in the chat. It does not ask questions: it decides by itself or pauses when something needs a human. When it pauses, the chat is waiting and the user answers in it.
8
+ */
6
9
  async function runAgentAutonomous () {
7
10
  const logger = new Logger()
8
11
  const currentUser = await CurrentUser.asyncFromDB('6ab56894eb20887d1ef7942c')
@@ -3,6 +3,9 @@ import { Logger } from 'openloops/common'
3
3
  import { CurrentUser } from 'openloops/base'
4
4
  import { AgiLoop } from 'openloops/loops'
5
5
 
6
+ /**
7
+ * Strict mode: when an approved MCP server changed, the agent always asks the user, even if the change looks harmless.
8
+ */
6
9
  async function runAgentInStrictMode () {
7
10
  const logger = new Logger()
8
11
  const currentUser = await CurrentUser.asyncFromDB('6ab56894eb20887d1ef7942c')
@@ -4,6 +4,9 @@ import { CurrentUser } from 'openloops/base'
4
4
  import { AgiLoop } from 'openloops/loops'
5
5
  import { Tool } from 'openloops/tools'
6
6
 
7
+ /**
8
+ * A destructive tool. Before it runs, the agent asks the user to confirm.
9
+ */
7
10
  const deleteNoteTool = new Tool({
8
11
  name: 'delete_note',
9
12
  description: 'Permanently delete a note by its id',
@@ -18,6 +21,9 @@ const deleteNoteTool = new Tool({
18
21
  handler: async ({ noteId }: { noteId: string }) => ({ data: { deleted: noteId } }),
19
22
  })
20
23
 
24
+ /**
25
+ * Two runs of the same chat: the first one ends with a question for the user, the second one carries the answer and the tool runs.
26
+ */
21
27
  async function runAgentWithConfirmation () {
22
28
  const logger = new Logger()
23
29
  const currentUser = await CurrentUser.asyncFromDB('6ab56894eb20887d1ef7942c')
@@ -3,6 +3,9 @@ import { Logger } from 'openloops/common'
3
3
  import { CurrentUser } from 'openloops/base'
4
4
  import { AgiLoop } from 'openloops/loops'
5
5
 
6
+ /**
7
+ * Sets the model that is used when neither the run, the user nor the chat choose one.
8
+ */
6
9
  async function runAgentWithFallbackModel () {
7
10
  const logger = new Logger()
8
11
  const currentUser = await CurrentUser.asyncFromDB('6ab56894eb20887d1ef7942c')
@@ -4,6 +4,9 @@ import { CurrentUser } from 'openloops/base'
4
4
  import { AgiLoop } from 'openloops/loops'
5
5
  import { inputGuards } from 'openloops/guardrails'
6
6
 
7
+ /**
8
+ * Adds input guards, in the options and with `addGuard`. A blocked message gets the reply of the guard and never reaches the model.
9
+ */
7
10
  async function runAgentWithGuards () {
8
11
  const logger = new Logger()
9
12
  const currentUser = await CurrentUser.asyncFromDB('6ab56894eb20887d1ef7942c')
@@ -3,6 +3,9 @@ import { Logger } from 'openloops/common'
3
3
  import { CurrentUser } from 'openloops/base'
4
4
  import { AgiLoop } from 'openloops/loops'
5
5
 
6
+ /**
7
+ * Hooks that run before and after the execution, for example to send a notification when the run finishes and the user asked for it.
8
+ */
6
9
  async function runAgentWithHooks () {
7
10
  const logger = new Logger()
8
11
  const currentUser = await CurrentUser.asyncFromDB('6ab56894eb20887d1ef7942c')
@@ -47,6 +50,9 @@ async function runAgentWithHooks () {
47
50
  }
48
51
  }
49
52
 
53
+ /**
54
+ * Stands for the service of notifications of your app.
55
+ */
50
56
  async function sendNotificationMock (params: any) {
51
57
  const logger = new Logger()
52
58
  logger.info(params, '[Notification Service] Sending notification to user...')
@@ -3,6 +3,9 @@ import { Logger } from 'openloops/common'
3
3
  import { CurrentUser } from 'openloops/base'
4
4
  import { AgiLoop } from 'openloops/loops'
5
5
 
6
+ /**
7
+ * Gives the agent a name, a gender and a role, so it answers as that persona.
8
+ */
6
9
  async function runAgentWithIdentity () {
7
10
  const logger = new Logger()
8
11
  const currentUser = await CurrentUser.asyncFromDB('6ab56894eb20887d1ef7942c')
@@ -3,6 +3,9 @@ import { Logger } from 'openloops/common'
3
3
  import { CurrentUser } from 'openloops/base'
4
4
  import { AgiLoop } from 'openloops/loops'
5
5
 
6
+ /**
7
+ * Chooses the model of this run. The chat saves the model that it used.
8
+ */
6
9
  async function runAgentWithModel () {
7
10
  const logger = new Logger()
8
11
  const currentUser = await CurrentUser.asyncFromDB('6ab56894eb20887d1ef7942c')
@@ -4,6 +4,9 @@ import { CurrentUser } from 'openloops/base'
4
4
  import { AgiLoop } from 'openloops/loops'
5
5
  import { webSearchTool, sendSlackMessageTool } from 'openloops/tools'
6
6
 
7
+ /**
8
+ * Adds two tools: a web search and a message to Slack. The agent decides by itself when to use each one.
9
+ */
7
10
  async function runAgentWithSlack () {
8
11
  const logger = new Logger()
9
12
  const currentUser = await CurrentUser.asyncFromDB('6ab56894eb20887d1ef7942c')
@@ -4,6 +4,9 @@ import { CurrentUser } from 'openloops/base'
4
4
  import { AgiLoop } from 'openloops/loops'
5
5
  import { webSearchTool } from 'openloops/tools'
6
6
 
7
+ /**
8
+ * Adds a tool to the agent. The model decides when to call it, and you can add more with `addTool` or in the options.
9
+ */
7
10
  async function runAgentWithTools () {
8
11
  const logger = new Logger()
9
12
  const currentUser = await CurrentUser.asyncFromDB('6ab56894eb20887d1ef7942c')
@@ -10,6 +10,9 @@ interface Output {
10
10
  }
11
11
  }
12
12
 
13
+ /**
14
+ * A basic tool. The model reads `name`, `description` and `parameters` to decide when to call it, and the handler returns `{ data }`. It is not destructive, so it runs without asking.
15
+ */
13
16
  export const greetUserTool = new Tool({
14
17
  name: 'greet_user',
15
18
  description: 'Greet the user',
@@ -10,6 +10,9 @@ interface Output {
10
10
  }
11
11
  }
12
12
 
13
+ /**
14
+ * A destructive tool: `destructive: true` makes the agent ask the user to confirm before it runs.
15
+ */
13
16
  export const deleteNoteTool = new Tool({
14
17
  name: 'delete_note',
15
18
  description: 'Permanently delete a note by its id',
@@ -11,6 +11,9 @@ interface Output {
11
11
  data: SerperSearchResponse | undefined
12
12
  }
13
13
 
14
+ /**
15
+ * A tool that calls an external service. It needs `SERPER_API_KEY` in the environment.
16
+ */
14
17
  export const webSearchTool = new Tool({
15
18
  name: 'web_search',
16
19
  description: 'Search on web',
@@ -2,6 +2,9 @@ import { Logger } from 'openloops/common'
2
2
  import { CurrentUser } from 'openloops/base'
3
3
  import { getLLMUsageByChat } from 'openloops/llmcalls'
4
4
 
5
+ /**
6
+ * Tokens and cost of a whole chat, with the breakdown by model.
7
+ */
5
8
  async function getChatUsage () {
6
9
  const logger = new Logger()
7
10
  const currentUser = await CurrentUser.asyncFromDB('6ab56894eb20887d1ef7942c')
@@ -2,6 +2,9 @@ import { Logger } from 'openloops/common'
2
2
  import { CurrentUser } from 'openloops/base'
3
3
  import { getLLMUsageByMessage } from 'openloops/llmcalls'
4
4
 
5
+ /**
6
+ * Tokens and cost of the answer to one message.
7
+ */
5
8
  async function getMessageUsage () {
6
9
  const logger = new Logger()
7
10
  const currentUser = await CurrentUser.asyncFromDB('6ab56894eb20887d1ef7942c')
@@ -2,6 +2,9 @@ import { Logger } from 'openloops/common'
2
2
  import { CurrentUser } from 'openloops/base'
3
3
  import { getLLMUsageStats } from 'openloops/llmcalls'
4
4
 
5
+ /**
6
+ * Usage between two dates, grouped by month and filtered by provider and model.
7
+ */
5
8
  async function getRangeStats () {
6
9
  const logger = new Logger()
7
10
  const currentUser = await CurrentUser.asyncFromDB('6ab56894eb20887d1ef7942c')
@@ -2,6 +2,9 @@ import { Logger } from 'openloops/common'
2
2
  import { CurrentUser } from 'openloops/base'
3
3
  import { getLLMUsageStats } from 'openloops/llmcalls'
4
4
 
5
+ /**
6
+ * Usage of the user in the current month, with the total, the breakdown by model and the series by day.
7
+ */
5
8
  async function getCurrentMonthStats () {
6
9
  const logger = new Logger()
7
10
  const currentUser = await CurrentUser.asyncFromDB('6ab56894eb20887d1ef7942c')
@@ -2,6 +2,9 @@ import { createUser, UserRole } from 'openloops/users'
2
2
  import { Scope } from 'openloops/base'
3
3
  import { Logger } from 'openloops/common'
4
4
 
5
+ /**
6
+ * Creates a user. There is no session yet, so the call uses a system scope with a reason.
7
+ */
5
8
  async function createUserExample () {
6
9
  const logger = new Logger()
7
10
 
@@ -2,6 +2,9 @@ import { Logger } from 'openloops/common'
2
2
  import { deleteUser } from 'openloops/users'
3
3
  import { CurrentUser } from 'openloops/base'
4
4
 
5
+ /**
6
+ * Deletes the account of a user without removing it from the database. The reason is optional.
7
+ */
5
8
  async function softDeleteUser () {
6
9
  const logger = new Logger()
7
10
  const currentUser = await CurrentUser.asyncFromDB('6ab56894eb20887d1ef7942c')
@@ -16,6 +19,9 @@ async function softDeleteUser () {
16
19
  logger.info(result, '[Demo] Deleted user')
17
20
  }
18
21
 
22
+ /**
23
+ * Removes the user from the database for good. Only an admin can do it.
24
+ */
19
25
  async function hardDeleteUser () {
20
26
  const logger = new Logger()
21
27
  const currentUser = await CurrentUser.asyncFromDB('6ab56894eb20887d1ef7942c')
@@ -3,6 +3,9 @@ import { getUserById } from 'openloops/users'
3
3
  import { CurrentUser } from 'openloops/base'
4
4
 
5
5
  // Note: return the user without the password field
6
+ /**
7
+ * Gets a user by id. The password is never returned.
8
+ */
6
9
  async function getUserByIdExample () {
7
10
  const logger = new Logger()
8
11
  const currentUser = await CurrentUser.asyncFromDB('6ab56894eb20887d1ef7942c')
@@ -2,6 +2,9 @@ import { Logger } from 'openloops/common'
2
2
  import { ListUsersByProps } from 'openloops/users'
3
3
  import { CurrentUser } from 'openloops/base'
4
4
 
5
+ /**
6
+ * Lists users by their properties, in pages. Only an admin can list other people.
7
+ */
5
8
  async function listUsers () {
6
9
  const logger = new Logger()
7
10
  const currentUser = await CurrentUser.asyncFromDB('6ab56894eb20887d1ef7942c')
@@ -2,6 +2,9 @@ import { Logger } from 'openloops/common'
2
2
  import { updateUser } from 'openloops/users'
3
3
  import { CurrentUser } from 'openloops/base'
4
4
 
5
+ /**
6
+ * Sets the default model of the user, used when a run or a chat does not choose one.
7
+ */
5
8
  async function setDefaultModel () {
6
9
  const logger = new Logger()
7
10
  const currentUser = await CurrentUser.asyncFromDB('6ab56894eb20887d1ef7942c')
@@ -19,6 +22,9 @@ async function setDefaultModel () {
19
22
  logger.info(result.preferences?.defaultModel, '[Demo] Default model:')
20
23
  }
21
24
 
25
+ /**
26
+ * Removes the default model of the user.
27
+ */
22
28
  async function removeDefaultModel () {
23
29
  const currentUser = await CurrentUser.asyncFromDB('6ab56894eb20887d1ef7942c')
24
30
 
@@ -2,6 +2,9 @@ import { Logger } from 'openloops/common'
2
2
  import { updateUser } from 'openloops/users'
3
3
  import { CurrentUser } from 'openloops/base'
4
4
 
5
+ /**
6
+ * Updates the profile of the user. To change the password use `changePassword`.
7
+ */
5
8
  async function updateUserExample () {
6
9
  const logger = new Logger()
7
10
  const currentUser = await CurrentUser.asyncFromDB('6ab56894eb20887d1ef7942c')
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "openloops",
3
- "version": "0.7.2",
3
+ "version": "0.7.3",
4
4
  "description": "Open source harness for AI agents: a TypeScript runtime with persistence, tools, MCP trust, approvals and autonomous automations, where the loop is pluggable",
5
5
  "author": {
6
6
  "name": "Jose Barcos",