# SDKを使った構築

Hugging Face のエージェント型 SDK を使用して、MCP 対応のエージェントを構築します。`huggingface_hub`（Python）および `@huggingface/tiny-agents`（JavaScript）ライブラリは、LLM を MCP ツールに接続するために必要なすべてを提供します。

## インストール

```bash
pip install "huggingface_hub[mcp]"
```

```bash
npm install @huggingface/tiny-agents
# or
pnpm add @huggingface/tiny-agents
```

## クイックスタート: エージェントの実行

最も素早く始めるには、`tiny-agents` CLI を使用します:

```bash
tiny-agents run julien-c/flux-schnell-generator
```

```bash
npx @huggingface/tiny-agents run "julien-c/flux-schnell-generator"
```

これは[tiny-agents コレクション](https://huggingface.co/datasets/tiny-agents/tiny-agents)からエージェントを読み込み、その MCP サーバーに接続して、対話型チャットを開始します。

## Agentクラスの使用

`Agent` クラスは、チャットループと MCP ツールの実行を管理します。LLM の実行には [Inference Providers](https://huggingface.co/docs/inference-providers) を使用します。

```python
from huggingface_hub import Agent
import asyncio

agent = Agent(
    model="Qwen/Qwen2.5-72B-Instruct",
    provider="novita",
    servers=[
        {
            "type": "sse",
            "url": "https://evalstate-flux1-schnell.hf.space/gradio_api/mcp/sse"
        }
    ]
)

async def main():
    async for chunk in agent.run("Generate an image of a sunset"):
        if hasattr(chunk, 'choices'):
            delta = chunk.choices[0].delta
            if delta.content:
                print(delta.content, end="")

asyncio.run(main())
```

すべてのオプションについては、[Agent リファレンス](https://huggingface.co/docs/huggingface_hub/package_reference/mcp#huggingface_hub.Agent)を参照してください。

```typescript
import { Agent } from "@huggingface/tiny-agents";

const agent = new Agent({
    model: "Qwen/Qwen2.5-72B-Instruct",
    provider: "novita",
    apiKey: process.env.HF_TOKEN,
    servers: [
        {
            type: "sse",
            url: "https://evalstate-flux1-schnell.hf.space/gradio_api/mcp/sse"
        }
    ]
});

await agent.loadTools();

for await (const chunk of agent.run("Generate an image of a sunset")) {
    if ("choices" in chunk) {
        const delta = chunk.choices[0]?.delta;
        if (delta.content) {
            console.log(delta.content);
        }
    }
}
```

すべてのオプションについては、[tiny-agentsのドキュメント](https://huggingface.co/docs/huggingface.js/tiny-agents/README)を参照してください。

## MCPClientを直接使用する

より詳細な制御を行うには、`MCPClient`を使用してMCPサーバーとツール呼び出しを直接管理してください。

```python
import asyncio
from huggingface_hub import MCPClient

async def main():
    async with MCPClient(
        model="Qwen/Qwen2.5-72B-Instruct",
        provider="novita",
    ) as client:
        # Connect to an MCP server
        await client.add_mcp_server(
            type="sse", 
            url="https://evalstate-flux1-schnell.hf.space/gradio_api/mcp/sse"
        )
        
        # Process a request with tools
        messages = [{"role": "user", "content": "Generate an image of a sunset"}]
        
        async for chunk in client.process_single_turn_with_tools(messages):
            if hasattr(chunk, 'choices'):
                delta = chunk.choices[0].delta
                if delta.content:
                    print(delta.content, end="")

asyncio.run(main())
```

すべてのオプションについては、[MCPClientの参照](https://huggingface.co/docs/huggingface_hub/package_reference/mcp#huggingface_hub.MCPClient)を参照してください。

JavaScript SDKは、MCPのインタラクションに`Agent`クラスを使用します。より低レベルの制御については、[@huggingface/mcp-client](https://huggingface.co/docs/huggingface.js/mcp-client/README)パッケージを参照してください。

## エージェントを共有する

Hub上の[tiny-agentsコレクション](https://huggingface.co/datasets/tiny-agents/tiny-agents)にエージェントを貢献してください。以下を含めてください:

- `agent.json` - エージェント設定（必須）
- `PROMPT.md`または`AGENTS.md` - システムプロンプト（任意）
- `EXAMPLES.md` - サンプルプロンプトとユースケース（任意）

## 詳細を見る

- [huggingface_hub MCP リファレンス](https://huggingface.co/docs/huggingface_hub/package_reference/mcp) - Python API リファレンス
- [tiny-agents ドキュメント](https://huggingface.co/docs/huggingface.js/tiny-agents/README) - JavaScript API リファレンス
- [推論プロバイダー](https://huggingface.co/docs/inference-providers) - 利用可能なLLMプロバイダー
- [tiny-agents コレクション](https://huggingface.co/datasets/tiny-agents/tiny-agents) - コミュニティのエージェントを閲覧
- [MCPサーバーガイド](./agents-mcp) - Hugging Face MCPサーバーに接続する

