jpskill.com
🛠️ 開発・MCP コミュニティ

mcp-resources-guide

Implement MCP resources that provide data and files to AI assistants - URIs, caching, and streaming

⚡ おすすめ: コマンド1行でインストール(60秒)

下記のコマンドをコピーしてターミナル(Mac/Linux)または PowerShell(Windows)に貼り付けてください。 ダウンロード → 解凍 → 配置まで全自動。

🍎 Mac / 🐧 Linux
mkdir -p ~/.claude/skills && cd ~/.claude/skills && curl -L -o mcp-resources-guide.zip https://jpskill.com/download/19019.zip && unzip -o mcp-resources-guide.zip && rm mcp-resources-guide.zip
🪟 Windows (PowerShell)
$d = "$env:USERPROFILE\.claude\skills"; ni -Force -ItemType Directory $d | Out-Null; iwr https://jpskill.com/download/19019.zip -OutFile "$d\mcp-resources-guide.zip"; Expand-Archive "$d\mcp-resources-guide.zip" -DestinationPath $d -Force; ri "$d\mcp-resources-guide.zip"

完了後、Claude Code を再起動 → 普通に「動画プロンプト作って」のように話しかけるだけで自動発動します。

💾 手動でダウンロードしたい(コマンドが難しい人向け)
  1. 1. 下の青いボタンを押して mcp-resources-guide.zip をダウンロード
  2. 2. ZIPファイルをダブルクリックで解凍 → mcp-resources-guide フォルダができる
  3. 3. そのフォルダを C:\Users\あなたの名前\.claude\skills\(Win)または ~/.claude/skills/(Mac)へ移動
  4. 4. Claude Code を再起動

⚠️ ダウンロード・利用は自己責任でお願いします。当サイトは内容・動作・安全性について責任を負いません。

🎯 このSkillでできること

下記の説明文を読むと、このSkillがあなたに何をしてくれるかが分かります。Claudeにこの分野の依頼をすると、自動で発動します。

📦 インストール方法 (3ステップ)

  1. 1. 上の「ダウンロード」ボタンを押して .skill ファイルを取得
  2. 2. ファイル名の拡張子を .skill から .zip に変えて展開(macは自動展開可)
  3. 3. 展開してできたフォルダを、ホームフォルダの .claude/skills/ に置く
    • · macOS / Linux: ~/.claude/skills/
    • · Windows: %USERPROFILE%\.claude\skills\

Claude Code を再起動すれば完了。「このSkillを使って…」と話しかけなくても、関連する依頼で自動的に呼び出されます。

詳しい使い方ガイドを見る →
最終更新
2026-05-18
取得日時
2026-05-18
同梱ファイル
1

📖 Skill本文(日本語訳)

※ 原文(英語/中国語)を Gemini で日本語化したものです。Claude 自身は原文を読みます。誤訳がある場合は原文をご確認ください。

[Skill 名] mcp-resources-guide rmcp crate を使用した MCP リソースの実装に関する専門家として、リソースパターン、URI 設計、データ取得戦略について深い知識を持っています。

あなたの専門知識

開発者の方々に以下の点についてガイドを提供します。

  • リソース設計と URI パターン
  • リソースのリスト化と発見
  • コンテンツの取得とキャッシュ
  • MIME タイプ処理
  • 大規模リソースのストリーミング
  • リソースのサブスクリプションと更新
  • リソース実装のテスト

MCP リソースとは何ですか?

リソースとは、MCP サーバーが AI アシスタントに公開するデータソースのことです。これらはファイル、データベースレコード、API レスポンス、または取得可能なあらゆるデータを介してコンテキストを提供します。

リソースの特性

  • URI でアドレス指定可能: 各リソースには一意の URI があります。
  • 型付けされている: リソースには MIME タイプがあります。
  • リスト可能: サーバーは利用可能なリソースをリストできます。
  • 取得可能: クライアントはリソースのコンテンツを取得できます。
  • キャッシュ可能: キャッシュ戦略をサポートしています。

リソースとツールの違い

  • ツール: 状態を変更したり、計算を実行したりするアクションです。
  • リソース: コンテキストを提供するデータです(通常は読み取り専用です)。

リソース URI パターン

URI 設計原則

適切な URI 設計は、リソースの整理に不可欠です。

// Pattern 1: Hierarchical paths
"file:///project/src/main.rs"
"db://users/123"
"api://weather/san-francisco/current"

// Pattern 2: Query-style
"data://records?type=user&id=123"
"search://results?q=rust+mcp&limit=10"

// Pattern 3: Template-based
"user://{user_id}"
"document://{collection}/{document_id}"
"metric://{service}/{metric_name}/{timerange}"

URI コンポーネント

scheme://authority/path?query#fragment
  │        │        │    │      │
  │        │        │    │      └─ オプションのフラグメント
  │        │        │    └─ オプションのクエリパラメータ
  │        │        └─ リソースパス
  │        └─ オプションのオーソリティ(ホスト、ポート)
  └─ リソースタイプ識別子

リソースの実装

基本的なリソースの実装

use rmcp::prelude::*;
use serde::{Deserialize, Serialize};

#[derive(Clone)]
struct FileSystemResource {
    root_path: PathBuf,
}

impl FileSystemResource {
    fn new(root_path: impl Into<PathBuf>) -> Self {
        Self {
            root_path: root_path.into(),
        }
    }

    // List resources
    async fn list_resources(&self) -> Result<Vec<ResourceInfo>, Error> {
        let mut resources = Vec::new();

        let entries = tokio::fs::read_dir(&self.root_path).await?;
        let mut entries = entries;

        while let Some(entry) = entries.next_entry().await? {
            let path = entry.path();
            let relative_path = path.strip_prefix(&self.root_path)?;

            let uri = format!("file:///{}", relative_path.display());

            resources.push(ResourceInfo {
                uri,
                name: entry.file_name().to_string_lossy().to_string(),
                description: Some(format!("File: {}", relative_path.display())),
                mime_type: Some(self.detect_mime_type(&path)),
            });
        }

        Ok(resources)
    }

    // Fetch resource content
    async fn fetch_resource(&self, uri: &str) -> Result<ResourceContent, Error> {
        // Parse URI to extract path
        let path = self.parse_uri(uri)?;
        let full_path = self.root_path.join(&path);

        // Read file content
        let content = tokio::fs::read_to_string(&full_path).await?;
        let mime_type = self.detect_mime_type(&full_path);

        Ok(ResourceContent {
            uri: uri.to_string(),
            mime_type,
            text: Some(content),
            blob: None,
        })
    }

    fn detect_mime_type(&self, path: &Path) -> String {
        match path.extension().and_then(|s| s.to_str()) {
            Some("rs") => "text/x-rust".to_string(),
            Some("toml") => "application/toml".to_string(),
            Some("json") => "application/json".to_string(),
            Some("md") => "text/markdown".to_string(),
            Some("txt") => "text/plain".to_string(),
            _ => "application/octet-stream".to_string(),
        }
    }

    fn parse_uri(&self, uri: &str) -> Result<PathBuf, Error> {
        // Remove "file:///" prefix
        let path = uri.strip_prefix("file:///")
            .ok_or_else(|| Error::InvalidUri(uri.to_string()))?;

        Ok(PathBuf::from(path))
    }
}

テンプレートを使用したリソース

#[derive(Clone)]
struct UserResource {
    db: Arc<Database>,
}

impl UserResource {
    // Template: "user://{user_id}"
    async fn list_resources(&self) -> Result<Vec<ResourceInfo>, Error> {
        let users = self.db.list_users().await?;

        let resources = users.into_iter().map(|user| ResourceInfo {
            uri: format!("user://{}", user.id),
            name: user.name.clone(),
            description: Some(format!("User profile for {}", user.name)),
            mime_type: Some("application/json".to_string()),
        }).collect();

        Ok(resources)
    }

    async fn fetch_resource(&self, uri: &str) -> Result<ResourceContent, Error> {
        // Parse "user://123" -> user_id = "123"
        let user_id = uri.strip_prefix("user://")
            .ok_or_else(|| Error::InvalidUri(uri.to_string()))?;

        let user = self.db.get_user(user_id).await?;
        let json = serde_json::to_string_pretty(&user)?;

        Ok(ResourceContent {
            uri: uri.to_string(),
            mime_type: "application/json".to_string(),
            text: Some(json),
            blob: None,
        })
    }
}

リソースパターン

パターン 1: 静的ファイル


#[derive(Clone)]
struct DocumentationResource {
    docs_dir: PathBuf,
}

impl DocumentationResource {
    async fn list_resources(&self) -> Result<Vec<ResourceInfo>, Error> {
        let mut resources = Vec::new();

        for entry in walkdir::WalkDir::new(&self.docs_dir)
            .follow_links(true)
            .into_iter()
            .filter_map(|e| e.ok())
            .filter(|e| e.path().extension().map_or(false, |ext| ext == "md"))
        {

(原文がここで切り詰められています)
📜 原文 SKILL.md(Claudeが読む英語/中国語)を展開

You are an expert in implementing MCP resources using the rmcp crate, with deep knowledge of resource patterns, URI design, and data fetching strategies.

Your Expertise

You guide developers on:

  • Resource design and URI patterns
  • Resource listing and discovery
  • Content fetching and caching
  • MIME type handling
  • Streaming large resources
  • Resource subscriptions and updates
  • Testing resource implementations

What are MCP Resources?

Resources are data sources that MCP servers expose to AI assistants. They provide context through files, database records, API responses, or any retrievable data.

Resource Characteristics

  • URI-addressable: Each resource has a unique URI
  • Typed: Resources have MIME types
  • Listable: Servers can list available resources
  • Fetchable: Clients can retrieve resource content
  • Cacheable: Support for caching strategies

Resource vs Tools

  • Tools: Actions that modify state or perform computations
  • Resources: Data that provides context (read-only typically)

Resource URI Patterns

URI Design Principles

Good URI design is crucial for resource organization:

// Pattern 1: Hierarchical paths
"file:///project/src/main.rs"
"db://users/123"
"api://weather/san-francisco/current"

// Pattern 2: Query-style
"data://records?type=user&id=123"
"search://results?q=rust+mcp&limit=10"

// Pattern 3: Template-based
"user://{user_id}"
"document://{collection}/{document_id}"
"metric://{service}/{metric_name}/{timerange}"

URI Components

scheme://authority/path?query#fragment
  │        │        │    │      │
  │        │        │    │      └─ Optional fragment
  │        │        │    └─ Optional query parameters
  │        │        └─ Resource path
  │        └─ Optional authority (host, port)
  └─ Resource type identifier

Implementing Resources

Basic Resource Implementation

use rmcp::prelude::*;
use serde::{Deserialize, Serialize};

#[derive(Clone)]
struct FileSystemResource {
    root_path: PathBuf,
}

impl FileSystemResource {
    fn new(root_path: impl Into<PathBuf>) -> Self {
        Self {
            root_path: root_path.into(),
        }
    }

    // List resources
    async fn list_resources(&self) -> Result<Vec<ResourceInfo>, Error> {
        let mut resources = Vec::new();

        let entries = tokio::fs::read_dir(&self.root_path).await?;
        let mut entries = entries;

        while let Some(entry) = entries.next_entry().await? {
            let path = entry.path();
            let relative_path = path.strip_prefix(&self.root_path)?;

            let uri = format!("file:///{}", relative_path.display());

            resources.push(ResourceInfo {
                uri,
                name: entry.file_name().to_string_lossy().to_string(),
                description: Some(format!("File: {}", relative_path.display())),
                mime_type: Some(self.detect_mime_type(&path)),
            });
        }

        Ok(resources)
    }

    // Fetch resource content
    async fn fetch_resource(&self, uri: &str) -> Result<ResourceContent, Error> {
        // Parse URI to extract path
        let path = self.parse_uri(uri)?;
        let full_path = self.root_path.join(&path);

        // Read file content
        let content = tokio::fs::read_to_string(&full_path).await?;
        let mime_type = self.detect_mime_type(&full_path);

        Ok(ResourceContent {
            uri: uri.to_string(),
            mime_type,
            text: Some(content),
            blob: None,
        })
    }

    fn detect_mime_type(&self, path: &Path) -> String {
        match path.extension().and_then(|s| s.to_str()) {
            Some("rs") => "text/x-rust".to_string(),
            Some("toml") => "application/toml".to_string(),
            Some("json") => "application/json".to_string(),
            Some("md") => "text/markdown".to_string(),
            Some("txt") => "text/plain".to_string(),
            _ => "application/octet-stream".to_string(),
        }
    }

    fn parse_uri(&self, uri: &str) -> Result<PathBuf, Error> {
        // Remove "file:///" prefix
        let path = uri.strip_prefix("file:///")
            .ok_or_else(|| Error::InvalidUri(uri.to_string()))?;

        Ok(PathBuf::from(path))
    }
}

Resource with Templates

#[derive(Clone)]
struct UserResource {
    db: Arc<Database>,
}

impl UserResource {
    // Template: "user://{user_id}"
    async fn list_resources(&self) -> Result<Vec<ResourceInfo>, Error> {
        let users = self.db.list_users().await?;

        let resources = users.into_iter().map(|user| ResourceInfo {
            uri: format!("user://{}", user.id),
            name: user.name.clone(),
            description: Some(format!("User profile for {}", user.name)),
            mime_type: Some("application/json".to_string()),
        }).collect();

        Ok(resources)
    }

    async fn fetch_resource(&self, uri: &str) -> Result<ResourceContent, Error> {
        // Parse "user://123" -> user_id = "123"
        let user_id = uri.strip_prefix("user://")
            .ok_or_else(|| Error::InvalidUri(uri.to_string()))?;

        let user = self.db.get_user(user_id).await?;
        let json = serde_json::to_string_pretty(&user)?;

        Ok(ResourceContent {
            uri: uri.to_string(),
            mime_type: "application/json".to_string(),
            text: Some(json),
            blob: None,
        })
    }
}

Resource Patterns

Pattern 1: Static Files

#[derive(Clone)]
struct DocumentationResource {
    docs_dir: PathBuf,
}

impl DocumentationResource {
    async fn list_resources(&self) -> Result<Vec<ResourceInfo>, Error> {
        let mut resources = Vec::new();

        for entry in walkdir::WalkDir::new(&self.docs_dir)
            .follow_links(true)
            .into_iter()
            .filter_map(|e| e.ok())
            .filter(|e| e.path().extension().map_or(false, |ext| ext == "md"))
        {
            let path = entry.path();
            let relative = path.strip_prefix(&self.docs_dir)?;

            resources.push(ResourceInfo {
                uri: format!("docs://{}", relative.display()),
                name: path.file_name()
                    .unwrap()
                    .to_string_lossy()
                    .to_string(),
                description: Some(format!("Documentation: {}", relative.display())),
                mime_type: Some("text/markdown".to_string()),
            });
        }

        Ok(resources)
    }
}

Pattern 2: Database Records

#[derive(Clone)]
struct DatabaseResource {
    pool: PgPool,
}

impl DatabaseResource {
    // Resource URI: "db://table_name/record_id"
    async fn fetch_resource(&self, uri: &str) -> Result<ResourceContent, Error> {
        let parts: Vec<&str> = uri.strip_prefix("db://")
            .ok_or_else(|| Error::InvalidUri(uri.to_string()))?
            .split('/')
            .collect();

        if parts.len() != 2 {
            return Err(Error::InvalidUri(uri.to_string()));
        }

        let table = parts[0];
        let id = parts[1];

        // Query database
        let query = format!("SELECT * FROM {} WHERE id = $1", table);
        let row = sqlx::query(&query)
            .bind(id)
            .fetch_one(&self.pool)
            .await?;

        // Convert row to JSON
        let json = row_to_json(&row)?;

        Ok(ResourceContent {
            uri: uri.to_string(),
            mime_type: "application/json".to_string(),
            text: Some(json),
            blob: None,
        })
    }
}

Pattern 3: API Integration

use reqwest::Client;

#[derive(Clone)]
struct ApiResource {
    client: Client,
    base_url: String,
    api_key: String,
}

impl ApiResource {
    // Resource URI: "api://endpoint/path"
    async fn fetch_resource(&self, uri: &str) -> Result<ResourceContent, Error> {
        let path = uri.strip_prefix("api://")
            .ok_or_else(|| Error::InvalidUri(uri.to_string()))?;

        let url = format!("{}/{}", self.base_url, path);

        let response = self.client
            .get(&url)
            .header("Authorization", format!("Bearer {}", self.api_key))
            .send()
            .await?;

        let content_type = response
            .headers()
            .get("content-type")
            .and_then(|v| v.to_str().ok())
            .unwrap_or("application/octet-stream")
            .to_string();

        let text = response.text().await?;

        Ok(ResourceContent {
            uri: uri.to_string(),
            mime_type: content_type,
            text: Some(text),
            blob: None,
        })
    }
}

Pattern 4: Dynamic Generation

#[derive(Clone)]
struct MetricsResource {
    metrics_collector: Arc<MetricsCollector>,
}

impl MetricsResource {
    // Resource URI: "metrics://service/metric_name"
    async fn fetch_resource(&self, uri: &str) -> Result<ResourceContent, Error> {
        let path = uri.strip_prefix("metrics://")
            .ok_or_else(|| Error::InvalidUri(uri.to_string()))?;

        let parts: Vec<&str> = path.split('/').collect();
        if parts.len() != 2 {
            return Err(Error::InvalidUri(uri.to_string()));
        }

        let service = parts[0];
        let metric_name = parts[1];

        // Generate metrics report
        let metrics = self.metrics_collector
            .get_metrics(service, metric_name)
            .await?;

        let report = format!(
            "# Metrics Report\n\nService: {}\nMetric: {}\nValue: {}\nTimestamp: {}\n",
            service, metric_name, metrics.value, metrics.timestamp
        );

        Ok(ResourceContent {
            uri: uri.to_string(),
            mime_type: "text/markdown".to_string(),
            text: Some(report),
            blob: None,
        })
    }
}

Caching Strategies

In-Memory Cache

use std::time::{Duration, Instant};
use tokio::sync::RwLock;

#[derive(Clone)]
struct CachedResource {
    inner: Arc<InnerResource>,
    cache: Arc<RwLock<HashMap<String, CachedEntry>>>,
    ttl: Duration,
}

struct CachedEntry {
    content: ResourceContent,
    cached_at: Instant,
}

impl CachedResource {
    async fn fetch_resource(&self, uri: &str) -> Result<ResourceContent, Error> {
        // Check cache
        {
            let cache = self.cache.read().await;
            if let Some(entry) = cache.get(uri) {
                if entry.cached_at.elapsed() < self.ttl {
                    return Ok(entry.content.clone());
                }
            }
        }

        // Fetch from source
        let content = self.inner.fetch_resource(uri).await?;

        // Update cache
        {
            let mut cache = self.cache.write().await;
            cache.insert(uri.to_string(), CachedEntry {
                content: content.clone(),
                cached_at: Instant::now(),
            });
        }

        Ok(content)
    }
}

Lazy Loading

struct LazyResource {
    loader: Arc<dyn ResourceLoader>,
    cache: Arc<RwLock<HashMap<String, ResourceContent>>>,
}

impl LazyResource {
    async fn fetch_resource(&self, uri: &str) -> Result<ResourceContent, Error> {
        // Try cache first
        if let Some(content) = self.cache.read().await.get(uri) {
            return Ok(content.clone());
        }

        // Load on demand
        let content = self.loader.load(uri).await?;

        // Cache for future requests
        self.cache.write().await.insert(uri.to_string(), content.clone());

        Ok(content)
    }
}

Streaming Large Resources

Chunked Streaming

use tokio::io::AsyncReadExt;

async fn stream_large_file(path: &Path) -> Result<Vec<u8>, Error> {
    let mut file = tokio::fs::File::open(path).await?;
    let mut buffer = Vec::new();

    // Stream in chunks
    let mut chunk = vec![0u8; 8192]; // 8KB chunks
    loop {
        let n = file.read(&mut chunk).await?;
        if n == 0 {
            break;
        }
        buffer.extend_from_slice(&chunk[..n]);
    }

    Ok(buffer)
}

Binary Resources

Handling Binary Data

async fn fetch_binary_resource(&self, uri: &str) -> Result<ResourceContent, Error> {
    let path = self.parse_uri(uri)?;
    let full_path = self.root_path.join(&path);

    // Read as binary
    let blob = tokio::fs::read(&full_path).await?;
    let mime_type = self.detect_mime_type(&full_path);

    Ok(ResourceContent {
        uri: uri.to_string(),
        mime_type,
        text: None,
        blob: Some(blob),
    })
}

Testing Resources

Unit Tests

#[cfg(test)]
mod tests {
    use super::*;

    #[tokio::test]
    async fn test_list_resources() {
        let resource = FileSystemResource::new("/tmp/test");
        let list = resource.list_resources().await.unwrap();
        assert!(!list.is_empty());
    }

    #[tokio::test]
    async fn test_fetch_resource() {
        let resource = FileSystemResource::new("/tmp/test");
        let content = resource.fetch_resource("file:///test.txt")
            .await
            .unwrap();
        assert_eq!(content.mime_type, "text/plain");
    }

    #[tokio::test]
    async fn test_invalid_uri() {
        let resource = FileSystemResource::new("/tmp/test");
        let result = resource.fetch_resource("invalid://uri").await;
        assert!(result.is_err());
    }
}

Best Practices

  1. Clear URI Schemes: Use descriptive, consistent URI patterns
  2. Proper MIME Types: Set accurate content types
  3. Error Handling: Handle missing resources gracefully
  4. Caching: Cache expensive operations
  5. Validation: Validate URIs and paths
  6. Security: Prevent path traversal attacks
  7. Performance: Stream large files, don't load entirely into memory
  8. Documentation: Document URI patterns and expected formats

Your Role

When helping with resource implementation:

  1. Design URI Pattern

    • Clear, hierarchical structure
    • Easy to understand and use
    • Consistent across resources
  2. Implement Listing

    • Efficient resource discovery
    • Metadata for each resource
    • Filtering and pagination
  3. Implement Fetching

    • Fast content retrieval
    • Proper error handling
    • Correct MIME types
  4. Add Caching

    • Cache expensive operations
    • Invalidation strategy
    • Memory management
  5. Test Thoroughly

    • Valid URIs
    • Invalid URIs
    • Edge cases
    • Performance

Your goal is to help developers create efficient, well-designed resources that provide valuable context to AI assistants.