Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions docs/JPYC/AI-Agent-JPYC-ChatApp/section-2/lesson-1.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,20 @@ git clone https://github.com/<your_github_account_name>/jpyc-ai-agent
cd ai-agent-jpyc-chat
```

### 📦 依存関係のインストール
### 📦 依存関係のインストールとコンパイル

必要なパッケージをインストールします:

```bash
pnpm install

# JPYC SDK Coreパッケージをコンパイル
cd external/jpyc-sdk/packages/core
pnpm run compile
```

これで`external/jpyc-sdk/packages/core/dist`フォルダが作成され、TypeScriptの型定義とコンパイル済みのコードが生成されます。

### 📁 プロジェクト構造の作成

`ai-agent-jpyc-chat`以下のディレクトリ構造が以下のようになっているかを確認します。
Expand Down Expand Up @@ -72,7 +78,7 @@ mkdir -p src/components
`.env.local`ファイルを作成し、必要な環境変数を設定します:

```bash
cp .env.local .env.local.example
cp .env.local.example .env.local
```

以下の値を設定します。
Expand Down
10 changes: 10 additions & 0 deletions docs/JPYC/AI-Agent-JPYC-ChatApp/section-3/lesson-1.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ title: JPYC SDK Wrapperの実装

`external/mcp/src/jpyc/sdk.ts`ファイルを作成し、以下のコードを記述します。

まず、ファイルを作成します:

```bash
cd jpyc-ai-agent

touch external/mcp/src/jpyc/sdk.ts
```

以下のコードを記述します:

```typescript
import { JPYC, type IJPYC, SdkClient, type ISdkClient } from "@jpyc/sdk-core";
import type { Hex } from "viem";
Expand Down
42 changes: 32 additions & 10 deletions docs/JPYC/AI-Agent-JPYC-ChatApp/section-3/lesson-2.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@ MCPツールは、AI Agentが実行できる「関数」として機能します

`external/mcp/src/tools.ts`ファイルを作成し、以下のコードを記述します。

まず、ファイルを作成します:

```bash
cd jpyc-ai-agent

touch external/mcp/src/tools.ts
```
以下のコードを記述します:

```typescript
/**
* JPYC Tools for Mastra
Expand Down Expand Up @@ -88,16 +97,16 @@ export const jpycBalanceTool = createTool({
});

/**
* JPYC送信ツール
* JPYCトークンを指定したアドレスに送信します(現在選択されているテストネット)
* JPYC送金ツール
* JPYCトークンを指定したアドレスに送金します(現在選択されているテストネット)
*/
export const jpycTransferTool = createTool({
id: "jpyc_transfer",
description:
"JPYCトークンを指定したアドレスに送信します(現在選択されているテストネット)。例: 10 JPYCを0x123...に送る",
"JPYCトークンを指定したアドレスに送金します(現在選択されているテストネット)。例: 10 JPYCを0x123...に送る",
inputSchema: z.object({
to: z.string().describe("送信先のEthereumアドレス (0xから始まる42文字)"),
amount: z.number().describe("送信額(JPYC単位、例: 10)"),
amount: z.number().describe("送金額(JPYC単位、例: 10)"),
}),
execute: async ({ context }) => {
try {
Expand All @@ -106,7 +115,7 @@ export const jpycTransferTool = createTool({
const currentChain = getCurrentChain();
const chainName = getChainName(currentChain);

// SDKのtransferメソッドを呼び出してJPYCを送信する
// SDKのtransferメソッドを呼び出してJPYCを送金する
const txHash = await jpyc.transfer({
to: to as `0x${string}`,
value: amount,
Expand All @@ -116,7 +125,7 @@ export const jpycTransferTool = createTool({

return {
success: true,
message: `✅ ${amount} JPYCを ${to} に送信しました(${chainName})`,
message: `✅ ${amount} JPYCを ${to} に送金しました(${chainName})`,
transactionHash: txHash,
explorerUrl: `${explorerUrl}${txHash}`,
chain: currentChain,
Expand Down Expand Up @@ -181,20 +190,25 @@ export const jpycGetCurrentChainTool = createTool({
id: "jpyc_get_current_chain",
description:
"現在選択されているテストネットを取得します。ユーザーが「今どのチェーン?」「現在のネットワークは?」などと聞いた場合に使用します。",
inputSchema: z.object({}),
execute: async () => {
inputSchema: z.object({
_dummy: z.string().optional().describe("ダミーパラメータ(使用しません)"),
}),
execute: async ({ context }) => {
try {
// 現在接続中のチェーン情報を取得
const currentChain = getCurrentChain();
const chainName = getChainName(currentChain);

console.log(`jpyc_get_current_chain: chain=${currentChain}`);

return {
success: true,
chain: currentChain,
chainName: chainName,
address: getCurrentAddress(),
};
} catch (error: unknown) {
console.error(`jpyc_get_current_chain error:`, error);
return {
success: false,
error: error instanceof Error ? error.message : String(error),
Expand All @@ -211,15 +225,21 @@ export const jpycTotalSupplyTool = createTool({
id: "jpyc_total_supply",
description:
"現在選択されているテストネットでのJPYCの総供給量を照会します。ユーザーが「総供給量は?」「流通量を教えて」などと聞いた場合に使用します。",
inputSchema: z.object({}),
execute: async () => {
inputSchema: z.object({
_dummy: z.string().optional().describe("ダミーパラメータ(使用しません)"),
}),
execute: async ({ context }) => {
try {
// 現在接続中のチェーン情報を取得
const currentChain = getCurrentChain();
const chainName = getChainName(currentChain);
// SDKのtotalSupplyメソッドを呼び出して総供給量を取得する
const totalSupply = await jpyc.totalSupply();

console.log(
`jpyc_total_supply: totalSupply=${totalSupply} JPYC, chain=${currentChain}`,
);

return {
success: true,
totalSupply: `${totalSupply} JPYC`,
Expand All @@ -228,6 +248,7 @@ export const jpycTotalSupplyTool = createTool({
chainName: chainName,
};
} catch (error: unknown) {
console.error(`jpyc_total_supply error:`, error);
return {
success: false,
error: error instanceof Error ? error.message : String(error),
Expand All @@ -246,6 +267,7 @@ export const jpycTools = {
jpyc_get_current_chain: jpycGetCurrentChainTool,
jpyc_total_supply: jpycTotalSupplyTool,
};

```

### 💡 コードの解説
Expand Down
30 changes: 30 additions & 0 deletions docs/JPYC/AI-Agent-JPYC-ChatApp/section-3/lesson-3.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ MCPサーバーは、HTTP/SSE(Server-Sent Events)プロトコルを使用し

`external/mcp/src/index.ts`ファイルを作成し、以下のコードを記述します。

まず、ファイルを作成します:

```bash
cd jpyc-ai-agent

touch external/mcp/src/tools.ts
```

以下のコードを記述します:

```typescript
/**
* JPYC MCP Server - Standalone MCP Server
Expand Down Expand Up @@ -216,6 +226,26 @@ process.on("SIGTERM", () => {

`SIGTERM`や`SIGINT`(Ctrl+C)シグナルを受信したとき、サーバーを適切に終了します。これにより、リクエスト処理中にサーバーが強制終了されることを防ぎます。

### 🔑 環境変数の設定

MCPサーバーは**独立したプロセス**として動作するため、`external/mcp`ディレクトリに`.env`ファイルを作成する必要があります。

```bash
# external/mcpディレクトリに移動
cd external/mcp

# .env.exampleをコピーして.envを作成
cp .env.example .env
```

`.env`ファイルに環境変数を設定します:

```.env
PRIVATE_KEY=0x... # ウォレットの秘密鍵
```

**重要:** ルートディレクトリの`.env.local`とは別に、`external/mcp/.env`も必要です。

### 🧪 動作確認

MCPサーバーを起動して、動作を確認しましょう。
Expand Down
10 changes: 10 additions & 0 deletions docs/JPYC/AI-Agent-JPYC-ChatApp/section-4/lesson-1.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@ title: LLMモデルの設定

`src/lib/mastra/model/index.ts`ファイルを作成し、以下のコードを記述します。

まず、ファイルを作成します:

```bash
cd jpyc-ai-agent

touch src/lib/mastra/model/index.ts
```

以下のコードを記述します:

```typescript
import { anthropic } from "@ai-sdk/anthropic";
import { google } from "@ai-sdk/google";
Expand Down
10 changes: 10 additions & 0 deletions docs/JPYC/AI-Agent-JPYC-ChatApp/section-4/lesson-2.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ MCPクライアントは、AI AgentからMCPサーバーに接続し、ツール

`src/lib/mastra/mcp/client.ts`ファイルを作成し、以下のコードを記述します。

まず、ファイルを作成します:

```bash
cd jpyc-ai-agent

touch src/lib/mastra/mcp/client.ts
```

以下のコードを記述します:

```typescript
import { MCPClient } from "@mastra/mcp";

Expand Down
10 changes: 10 additions & 0 deletions docs/JPYC/AI-Agent-JPYC-ChatApp/section-4/lesson-3.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ AI Agentは、ユーザーの自然言語の指示を理解し、適切なMCPツ

`src/lib/mastra/agent.ts`ファイルを作成し、以下のコードを記述します。

まず、ファイルを作成します:

```bash
cd jpyc-ai-agent

touch src/lib/mastra/agent.ts
```

以下のコードを記述します:

```typescript
import { Agent } from "@mastra/core/agent";
import { claude } from "./model";
Expand Down
10 changes: 10 additions & 0 deletions docs/JPYC/AI-Agent-JPYC-ChatApp/section-5/lesson-1.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ title: チャットAPIの実装

`src/app/api/chat/route.ts`ファイルを作成し、以下のコードを記述します。

まず、ファイルを作成します:

```bash
cd jpyc-ai-agent

touch src/app/api/chat/route.ts
```

以下のコードを記述します:

```typescript
import { jpycAgent } from "@/lib/mastra/agent";
import type { NextRequest } from "next/server";
Expand Down
Loading