@aigne/example-mcp-blocklet 1.7.2 → 1.8.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/LICENSE ADDED
@@ -0,0 +1,93 @@
1
+ Elastic License 2.0
2
+
3
+ URL: https://www.elastic.co/licensing/elastic-license
4
+
5
+ ## Acceptance
6
+
7
+ By using the software, you agree to all of the terms and conditions below.
8
+
9
+ ## Copyright License
10
+
11
+ The licensor grants you a non-exclusive, royalty-free, worldwide,
12
+ non-sublicensable, non-transferable license to use, copy, distribute, make
13
+ available, and prepare derivative works of the software, in each case subject to
14
+ the limitations and conditions below.
15
+
16
+ ## Limitations
17
+
18
+ You may not provide the software to third parties as a hosted or managed
19
+ service, where the service provides users with access to any substantial set of
20
+ the features or functionality of the software.
21
+
22
+ You may not move, change, disable, or circumvent the license key functionality
23
+ in the software, and you may not remove or obscure any functionality in the
24
+ software that is protected by the license key.
25
+
26
+ You may not alter, remove, or obscure any licensing, copyright, or other notices
27
+ of the licensor in the software. Any use of the licensor’s trademarks is subject
28
+ to applicable law.
29
+
30
+ ## Patents
31
+
32
+ The licensor grants you a license, under any patent claims the licensor can
33
+ license, or becomes able to license, to make, have made, use, sell, offer for
34
+ sale, import and have imported the software, in each case subject to the
35
+ limitations and conditions in this license. This license does not cover any
36
+ patent claims that you cause to be infringed by modifications or additions to
37
+ the software. If you or your company make any written claim that the software
38
+ infringes or contributes to infringement of any patent, your patent license for
39
+ the software granted under these terms ends immediately. If your company makes
40
+ such a claim, your patent license ends immediately for work on behalf of your
41
+ company.
42
+
43
+ ## Notices
44
+
45
+ You must ensure that anyone who gets a copy of any part of the software from you
46
+ also gets a copy of these terms.
47
+
48
+ If you modify the software, you must include in any modified copies of the
49
+ software prominent notices stating that you have modified the software.
50
+
51
+ ## No Other Rights
52
+
53
+ These terms do not imply any licenses other than those expressly granted in
54
+ these terms.
55
+
56
+ ## Termination
57
+
58
+ If you use the software in violation of these terms, such use is not licensed,
59
+ and your licenses will automatically terminate. If the licensor provides you
60
+ with a notice of your violation, and you cease all violation of this license no
61
+ later than 30 days after you receive that notice, your licenses will be
62
+ reinstated retroactively. However, if you violate these terms after such
63
+ reinstatement, any additional violation of these terms will cause your licenses
64
+ to terminate automatically and permanently.
65
+
66
+ ## No Liability
67
+
68
+ *As far as the law allows, the software comes as is, without any warranty or
69
+ condition, and the licensor will not be liable to you for any damages arising
70
+ out of these terms or the use or nature of the software, under any kind of
71
+ legal claim.*
72
+
73
+ ## Definitions
74
+
75
+ The **licensor** is the entity offering these terms, and the **software** is the
76
+ software the licensor makes available under these terms, including any portion
77
+ of it.
78
+
79
+ **you** refers to the individual or entity agreeing to these terms.
80
+
81
+ **your company** is any legal entity, sole proprietorship, or other kind of
82
+ organization that you work for, plus all organizations that have control over,
83
+ are under the control of, or are under common control with that
84
+ organization. **control** means ownership of substantially all the assets of an
85
+ entity, or the power to direct its management and policies by vote, contract, or
86
+ otherwise. Control can be direct or indirect.
87
+
88
+ **your licenses** are all the licenses granted to you for the software under
89
+ these terms.
90
+
91
+ **use** means anything you do with the software requiring one of your licenses.
92
+
93
+ **trademark** means trademarks, service marks, and similar rights.
package/README.md CHANGED
@@ -49,6 +49,12 @@ BLOCKLET_APP_URL="" # Set your Blocklet app URL here
49
49
  pnpm start
50
50
  ```
51
51
 
52
+ or
53
+
54
+ ```bash
55
+ pnpm start https://your-blocklet-app-url
56
+ ```
57
+
52
58
  ## License
53
59
 
54
60
  This project is licensed under the MIT License.
package/index.ts CHANGED
@@ -2,11 +2,11 @@
2
2
 
3
3
  import assert from "node:assert";
4
4
  import { runChatLoopInTerminal } from "@aigne/cli/utils/run-chat-loop.js";
5
- import { AIAgent, ExecutionEngine, MCPAgent, PromptBuilder } from "@aigne/core";
5
+ import { AIAgent, AIGNE, MCPAgent, PromptBuilder } from "@aigne/core";
6
6
  import { loadModel } from "@aigne/core/loader/index.js";
7
7
  import { logger } from "@aigne/core/utils/logger.js";
8
8
  import { UnauthorizedError, refreshAuthorization } from "@modelcontextprotocol/sdk/client/auth.js";
9
- import { SSEClientTransport } from "@modelcontextprotocol/sdk/client/sse.js";
9
+ import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js";
10
10
  // @ts-ignore
11
11
  import JWT from "jsonwebtoken";
12
12
 
@@ -14,20 +14,28 @@ import { TerminalOAuthProvider } from "./oauth.js";
14
14
 
15
15
  logger.enable(`aigne:mcp,${process.env.DEBUG}`);
16
16
 
17
- const { BLOCKLET_APP_URL } = process.env;
18
- assert(BLOCKLET_APP_URL, "Please set the BLOCKLET_APP_URL environment variable");
19
- console.info("Connecting to blocklet app", BLOCKLET_APP_URL);
17
+ const rawUrl = process.argv[2] || process.env.BLOCKLET_APP_URL;
18
+ assert(
19
+ rawUrl,
20
+ "Please provide a blocklet url as an argument or set the BLOCKLET_APP_URL environment variable",
21
+ );
20
22
 
21
- const appUrl = new URL(BLOCKLET_APP_URL);
22
- appUrl.pathname = "/.well-known/service/mcp/sse";
23
+ const appUrl = new URL(rawUrl);
24
+ appUrl.pathname = "/.well-known/service/mcp";
25
+ console.info("Connecting to blocklet", appUrl.href);
26
+
27
+ let transport: StreamableHTTPClientTransport;
23
28
 
24
29
  const provider = new TerminalOAuthProvider(appUrl.host);
25
30
  const authCodePromise = new Promise((resolve, reject) => {
26
- provider.once("authorized", resolve);
31
+ provider.once("authorized", async (code) => {
32
+ await transport.finishAuth(code);
33
+ resolve(code);
34
+ });
27
35
  provider.once("error", reject);
28
36
  });
29
37
 
30
- const transport = new SSEClientTransport(appUrl, {
38
+ transport = new StreamableHTTPClientTransport(appUrl, {
31
39
  authProvider: provider,
32
40
  });
33
41
 
@@ -35,22 +43,25 @@ try {
35
43
  let tokens = await provider.tokens();
36
44
  if (tokens) {
37
45
  let decoded = JWT.decode(tokens.access_token);
38
- console.info("Decoded access token:", decoded);
39
46
  if (decoded) {
40
47
  const now = Date.now();
41
48
  const expiresAt = decoded.exp * 1000;
42
49
  if (now < expiresAt) {
43
50
  console.info("Tokens already exist and not expired, skipping authorization");
44
51
  } else if (tokens.refresh_token) {
52
+ console.info("Access token expired:", { now, expiresAt, decoded });
45
53
  decoded = JWT.decode(tokens.refresh_token);
46
- console.info("Decoded refresh token:", decoded);
47
54
  if (decoded) {
48
55
  const now = Date.now();
49
56
  const expiresAt = decoded.exp * 1000;
50
57
  if (now < expiresAt) {
51
58
  console.info("Refresh token already exists and not expired, refreshing authorization");
52
59
  try {
60
+ const oauthUrl = new URL(appUrl.href);
61
+ oauthUrl.pathname = "/.well-known/oauth-authorization-server";
62
+ const metadata = await fetch(oauthUrl.href).then((res) => res.json());
53
63
  tokens = await refreshAuthorization(appUrl.href, {
64
+ metadata,
54
65
  // biome-ignore lint/style/noNonNullAssertion: <explanation>
55
66
  clientInformation: (await provider.clientInformation())!,
56
67
  refreshToken: tokens.refresh_token,
@@ -72,7 +83,7 @@ try {
72
83
  }
73
84
  }
74
85
  } else {
75
- console.info("No tokens found, starting authorization");
86
+ console.info("No tokens found, starting authorization...");
76
87
  await transport.start();
77
88
  }
78
89
  } catch (error) {
@@ -87,21 +98,20 @@ try {
87
98
  }
88
99
  }
89
100
 
90
- console.info("Starting connecting to blocklet mcp...");
91
-
92
101
  const model = await loadModel();
93
102
 
94
103
  const blocklet = await MCPAgent.from({
95
104
  url: appUrl.href,
96
105
  timeout: 8000,
106
+ transport: "streamableHttp",
97
107
  opts: {
98
108
  authProvider: provider,
99
109
  },
100
110
  });
101
111
 
102
- const engine = new ExecutionEngine({
112
+ const aigne = new AIGNE({
103
113
  model,
104
- tools: [blocklet],
114
+ skills: [blocklet],
105
115
  });
106
116
 
107
117
  const agent = AIAgent.from({
@@ -111,7 +121,7 @@ const agent = AIAgent.from({
111
121
  memory: true,
112
122
  });
113
123
 
114
- const userAgent = engine.call(agent);
124
+ const userAgent = aigne.invoke(agent);
115
125
 
116
126
  await runChatLoopInTerminal(userAgent, {
117
127
  welcome:
package/oauth.ts CHANGED
@@ -147,8 +147,10 @@ export class TerminalOAuthProvider extends EventEmitter implements OAuthClientPr
147
147
 
148
148
  if (code) {
149
149
  this.emit("authorized", code);
150
- console.info("Authorization successful!", Date.now());
151
- resolve();
150
+ console.info("Authorization code received, exchanging for tokens...");
151
+ setTimeout(() => {
152
+ resolve();
153
+ }, 3000);
152
154
  } else {
153
155
  this.emit("error", new Error("No authorization code received"));
154
156
  reject(new Error("No authorization code received"));
@@ -158,7 +160,7 @@ export class TerminalOAuthProvider extends EventEmitter implements OAuthClientPr
158
160
 
159
161
  // Start the local server
160
162
  server.listen(this.localServerPort, async () => {
161
- console.log("Please authorize the application in your browser...");
163
+ console.log("Please complete the authorization in your browser...");
162
164
  // Open the authorization URL in the default browser
163
165
  await open(authorizationUrl.toString());
164
166
  });
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "@aigne/example-mcp-blocklet",
3
- "version": "1.7.2",
3
+ "version": "1.8.0",
4
4
  "description": "A demonstration of using AIGNE Framework and MCP Server hosted by the Blocklet platform",
5
5
  "author": "Arcblock <blocklet@arcblock.io> https://github.com/blocklet",
6
6
  "homepage": "https://github.com/AIGNE-io/aigne-framework/tree/main/examples/mcp-blocklet",
7
- "license": "ISC",
7
+ "license": "MIT",
8
8
  "repository": {
9
9
  "type": "git",
10
10
  "url": "git+https://github.com/AIGNE-io/aigne-framework"
@@ -20,8 +20,11 @@
20
20
  "jsonwebtoken": "^9.0.2",
21
21
  "open": "^10.1.0",
22
22
  "zod": "^3.24.2",
23
- "@aigne/cli": "^1.6.0",
24
- "@aigne/core": "^1.10.0"
23
+ "@aigne/core": "^1.12.0",
24
+ "@aigne/cli": "^1.8.0"
25
+ },
26
+ "devDependencies": {
27
+ "@aigne/test-utils": "^0.1.0"
25
28
  },
26
29
  "scripts": {
27
30
  "start": "bun run index.ts",