gyazo-mcp-server 0.6.3

Local MCP server for Gyazo with HTTP and stdio transport support
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
use anyhow::Result;
use rmcp::{
    ErrorData as McpError,
    handler::server::wrapper::Parameters,
    model::{CallToolResult, Content},
    service::RequestContext,
};
use schemars::JsonSchema;
use serde::Deserialize;
use serde_json::json;

use crate::{
    gyazo_api::{
        GyazoUploadImageRequest, delete_image, fetch_authenticated_user, fetch_image_as_base64,
        get_image, get_latest_image, get_oembed, list_images, search_images, upload_image,
    },
    server::GyazoServer,
};

#[derive(Debug, Deserialize, JsonSchema)]
struct GyazoListImagesArgs {
    page: Option<u32>,
    per_page: Option<u32>,
}

#[derive(Debug, Deserialize, JsonSchema)]
struct GyazoSearchArgs {
    query: String,
    page: Option<u32>,
    per: Option<u32>,
}

#[derive(Debug, Deserialize, JsonSchema)]
struct GyazoGetImageArgs {
    image_id: Option<String>,
    image_url: Option<String>,
}

#[derive(Debug, Deserialize, JsonSchema)]
struct GyazoOEmbedArgs {
    image_url: String,
    /// 組み立て済み `img_tag_html` の `<img>` タグに使う alt 属性。省略時は
    /// `"Gyazo image"`。Gyazo の oEmbed エンドポイントは title 等のリッチ
    /// メタデータを返さないため、呼び出し側で意味のある alt を指定したい
    /// 場合に使う。
    alt: Option<String>,
}

#[derive(Debug, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
struct GyazoUploadImageArgs {
    image_data: String,
    access_policy: Option<String>,
    metadata_is_public: Option<bool>,
    referer_url: Option<String>,
    app: Option<String>,
    title: Option<String>,
    description: Option<String>,
    created_at: Option<f64>,
    collection_id: Option<String>,
}

#[rmcp::tool_router(router = gyazo_tool_router, vis = "pub(crate)")]
impl GyazoServer {
    #[rmcp::tool(description = "現在の MCP access token に紐づく Gyazo ユーザーを表示します")]
    async fn gyazo_whoami(
        &self,
        request_context: RequestContext<rmcp::service::RoleServer>,
    ) -> Result<CallToolResult, McpError> {
        let session = self
            .authorized_session_for_request(&request_context)
            .await?;
        let user = fetch_authenticated_user(&session.record.backend_access_token)
            .await
            .map_err(internal_error)?;

        json_result(json!({
            "uid": user.uid,
            "name": user.name,
            "email": user.email,
            "profile_image": user.profile_image,
        }))
    }

    #[rmcp::tool(
        description = "現在の Gyazo ユーザーがアップロードしたキャプチャを全文検索します。各画像の戻り値に含まれる resource_uri (gyazo-mcp:///{image_id}) を MCP read_resource に渡すと、画像本体のバイナリを取得できます。"
    )]
    async fn gyazo_search(
        &self,
        request_context: RequestContext<rmcp::service::RoleServer>,
        Parameters(GyazoSearchArgs { query, page, per }): Parameters<GyazoSearchArgs>,
    ) -> Result<CallToolResult, McpError> {
        let session = self
            .authorized_session_for_request(&request_context)
            .await?;
        let images = search_images(&session.record.backend_access_token, &query, page, per)
            .await
            .map_err(internal_error)?;

        json_result(images)
    }

    #[rmcp::tool(
        description = "認証済みユーザーの Gyazo 画像一覧を取得します。各画像の戻り値に含まれる resource_uri (gyazo-mcp:///{image_id}) を MCP read_resource に渡すと、画像本体のバイナリを取得できます。"
    )]
    async fn gyazo_list_images(
        &self,
        request_context: RequestContext<rmcp::service::RoleServer>,
        Parameters(GyazoListImagesArgs { page, per_page }): Parameters<GyazoListImagesArgs>,
    ) -> Result<CallToolResult, McpError> {
        let session = self
            .authorized_session_for_request(&request_context)
            .await?;
        let images = list_images(&session.record.backend_access_token, page, per_page)
            .await
            .map_err(internal_error)?;

        json_result(images)
    }

    #[rmcp::tool(
        description = "画像 ID または Gyazo URL を指定して 1 件の画像を取得します。戻り値に含まれる resource_uri (gyazo-mcp:///{image_id}) を MCP read_resource に渡すと、画像本体のバイナリを取得できます。"
    )]
    async fn gyazo_get_image(
        &self,
        request_context: RequestContext<rmcp::service::RoleServer>,
        Parameters(args): Parameters<GyazoGetImageArgs>,
    ) -> Result<CallToolResult, McpError> {
        let session = self
            .authorized_session_for_request(&request_context)
            .await?;
        let image_ref = select_image_ref(args)?;
        let image = get_image(&session.record.backend_access_token, &image_ref)
            .await
            .map_err(internal_error)?;

        json_result(image)
    }

    #[rmcp::tool(description = "画像 ID または Gyazo URL を指定して 1 件の画像を削除します")]
    async fn gyazo_delete_image(
        &self,
        request_context: RequestContext<rmcp::service::RoleServer>,
        Parameters(args): Parameters<GyazoGetImageArgs>,
    ) -> Result<CallToolResult, McpError> {
        let session = self
            .authorized_session_for_request(&request_context)
            .await?;
        let image_ref = select_image_ref(args)?;
        let deleted = delete_image(&session.record.backend_access_token, &image_ref)
            .await
            .map_err(internal_error)?;

        json_result(deleted)
    }

    #[rmcp::tool(
        description = "最新の Gyazo 画像を画像本体とメタデータ付きで取得します。戻り値のメタデータには resource_uri (gyazo-mcp:///{image_id}) も含まれ、後から MCP read_resource に渡すことで画像本体のバイナリを再取得できます。"
    )]
    async fn gyazo_get_latest_image(
        &self,
        request_context: RequestContext<rmcp::service::RoleServer>,
    ) -> Result<CallToolResult, McpError> {
        let session = self
            .authorized_session_for_request(&request_context)
            .await?;
        let image = get_latest_image(&session.record.backend_access_token)
            .await
            .map_err(internal_error)?;
        let binary = fetch_image_as_base64(&image.url)
            .await
            .map_err(internal_error)?;

        Ok(CallToolResult::success(vec![
            Content::image(binary.data, binary.mime_type),
            Content::text(serde_json::to_string_pretty(&image).map_err(internal_error)?),
        ]))
    }

    #[rmcp::tool(description = "base64 画像を Gyazo にアップロードします")]
    async fn gyazo_upload_image(
        &self,
        request_context: RequestContext<rmcp::service::RoleServer>,
        Parameters(args): Parameters<GyazoUploadImageArgs>,
    ) -> Result<CallToolResult, McpError> {
        let session = self
            .authorized_session_for_request(&request_context)
            .await?;
        let uploaded = upload_image(
            &session.record.backend_access_token,
            GyazoUploadImageRequest {
                image_data: args.image_data,
                access_policy: args.access_policy,
                metadata_is_public: args.metadata_is_public,
                referer_url: args.referer_url,
                app: args.app,
                title: args.title,
                description: args.description,
                created_at: args.created_at,
                collection_id: args.collection_id,
            },
        )
        .await
        .map_err(internal_error)?;

        json_result(uploaded)
    }

    #[rmcp::tool(
        description = "Gyazo 画像ページ URL の oEmbed メタデータを取得します。戻り値の oembed_discovery_link は oEmbed spec 第 4 章で定義された discovery 用の <link rel=\"alternate\" type=\"application/json+oembed\" ...> タグで、HTML ページの <head> に置くと oEmbed 対応クライアントが埋め込み情報を発見できます。img_tag_html は spec 外の便利機能で、url / width / height と alt から組み立て済みの <img> タグです (markdown / HTML にそのまま貼って画像埋め込みに使えます)。oEmbed エンドポイントは title 等のリッチメタデータを返さないため、画像のタイトルや説明文等が必要な場合は gyazo_get_image を併用してください。"
    )]
    async fn gyazo_get_oembed_metadata(
        &self,
        Parameters(GyazoOEmbedArgs { image_url, alt }): Parameters<GyazoOEmbedArgs>,
    ) -> Result<CallToolResult, McpError> {
        let oembed = get_oembed(&image_url).await.map_err(internal_error)?;
        let img_tag_html = build_oembed_img_tag(
            &oembed.url,
            oembed.width,
            oembed.height,
            alt.as_deref().unwrap_or(DEFAULT_OEMBED_IMG_ALT),
        );
        let oembed_discovery_link = build_oembed_discovery_link(&image_url);

        json_result(json!({
            "version": oembed.version,
            "type": oembed.embed_type,
            "provider_name": oembed.provider_name,
            "provider_url": oembed.provider_url,
            "url": oembed.url,
            "width": oembed.width,
            "height": oembed.height,
            "oembed_discovery_link": oembed_discovery_link,
            "img_tag_html": img_tag_html,
        }))
    }
}

/// `gyazo_get_oembed_metadata` の `alt` 引数が省略されたときに
/// `img_tag_html` の `alt` 属性に使うデフォルト値。
const DEFAULT_OEMBED_IMG_ALT: &str = "Gyazo image";

/// oEmbed spec 第 4 章で定義された discovery link を組み立てる。
/// HTML ページの `<head>` に置くと、oEmbed 対応クライアント (クローラ等) が
/// この URL の埋め込み情報を発見できる。
fn build_oembed_discovery_link(image_page_url: &str) -> String {
    let encoded = percent_encode_query_value(image_page_url);
    format!(
        "<link rel=\"alternate\" type=\"application/json+oembed\" href=\"https://api.gyazo.com/api/oembed?url={encoded}\" title=\"Image shared with Gyazo\" />"
    )
}

/// `<img src="..." width="..." height="..." alt="..." />` を組み立てる。
/// `src` と `alt` は HTML 属性向けに最小限のエスケープ (`&`, `<`, `>`, `"`)
/// を行う。`width` / `height` は数値なのでエスケープ不要。
fn build_oembed_img_tag(src: &str, width: u64, height: u64, alt: &str) -> String {
    format!(
        "<img src=\"{src}\" width=\"{width}\" height=\"{height}\" alt=\"{alt}\" />",
        src = escape_html_attribute(src),
        alt = escape_html_attribute(alt),
    )
}

/// URL クエリ値向けの最小 percent エンコード。RFC 3986 の unreserved
/// 文字 (`A-Za-z0-9-._~`) 以外をすべて `%XX` に変換する。`&` や `=` も
/// エスケープされるので、エンコード後の文字列は HTML 属性内にそのまま
/// 埋め込んでも安全 (HTML エスケープが必要な特殊文字を含まない)。
fn percent_encode_query_value(value: &str) -> String {
    let mut encoded = String::with_capacity(value.len());
    for byte in value.bytes() {
        let unreserved = byte.is_ascii_alphanumeric()
            || byte == b'-'
            || byte == b'.'
            || byte == b'_'
            || byte == b'~';
        if unreserved {
            encoded.push(byte as char);
        } else {
            encoded.push_str(&format!("%{byte:02X}"));
        }
    }
    encoded
}

fn escape_html_attribute(value: &str) -> String {
    let mut escaped = String::with_capacity(value.len());
    for ch in value.chars() {
        match ch {
            '&' => escaped.push_str("&amp;"),
            '<' => escaped.push_str("&lt;"),
            '>' => escaped.push_str("&gt;"),
            '"' => escaped.push_str("&quot;"),
            _ => escaped.push(ch),
        }
    }
    escaped
}

fn select_image_ref(args: GyazoGetImageArgs) -> Result<String, McpError> {
    match (args.image_id, args.image_url) {
        (Some(image_id), None) if !image_id.trim().is_empty() => Ok(image_id),
        (None, Some(image_url)) if !image_url.trim().is_empty() => Ok(image_url),
        (Some(_), Some(_)) => Err(McpError::invalid_params(
            "image_id と image_url はどちらか一方のみ指定してください",
            None,
        )),
        _ => Err(McpError::invalid_params(
            "image_id か image_url のいずれかを指定してください",
            None,
        )),
    }
}

fn json_result<T: serde::Serialize>(value: T) -> Result<CallToolResult, McpError> {
    let text = serde_json::to_string_pretty(&value).map_err(internal_error)?;
    Ok(CallToolResult::success(vec![Content::text(text)]))
}

fn internal_error(error: impl std::fmt::Display) -> McpError {
    McpError::internal_error(error.to_string(), None)
}

#[cfg(test)]
mod tests {
    use super::{
        DEFAULT_OEMBED_IMG_ALT, build_oembed_discovery_link, build_oembed_img_tag,
        escape_html_attribute, percent_encode_query_value,
    };

    #[test]
    fn build_oembed_img_tag_uses_url_dimensions_and_alt() {
        let html = build_oembed_img_tag("https://i.gyazo.com/abc123.png", 640, 480, "screenshot");
        assert_eq!(
            html,
            "<img src=\"https://i.gyazo.com/abc123.png\" width=\"640\" height=\"480\" alt=\"screenshot\" />"
        );
    }

    #[test]
    fn build_oembed_img_tag_escapes_dangerous_chars_in_alt() {
        // alt にユーザー入力の "/<>/& が来ても属性脱出されないことを保証する。
        let html = build_oembed_img_tag("https://i.gyazo.com/abc.png", 10, 20, r#"a"b<c>d&e"#);
        assert!(html.contains("alt=\"a&quot;b&lt;c&gt;d&amp;e\""), "{html}");
        // 属性を閉じる素の `"` が alt 内に残っていないこと
        assert!(!html.contains("alt=\"a\""), "{html}");
    }

    #[test]
    fn build_oembed_img_tag_escapes_dangerous_chars_in_src() {
        // src は通常 Gyazo の URL なのでエスケープ不要だが、念のため & を含む
        // クエリ付き URL でも属性脱出しないことを保証する。
        let html = build_oembed_img_tag("https://i.gyazo.com/abc.png?a=1&b=2", 10, 20, "x");
        assert!(
            html.contains("src=\"https://i.gyazo.com/abc.png?a=1&amp;b=2\""),
            "{html}"
        );
    }

    #[test]
    fn escape_html_attribute_handles_all_special_chars() {
        assert_eq!(escape_html_attribute(r#"&<>"'"#), "&amp;&lt;&gt;&quot;'");
    }

    #[test]
    fn percent_encode_query_value_encodes_non_unreserved() {
        // Gyazo の典型的な画像ページ URL を percent-encode した結果を保証する。
        let encoded = percent_encode_query_value("https://gyazo.com/abc123");
        assert_eq!(encoded, "https%3A%2F%2Fgyazo.com%2Fabc123");
    }

    #[test]
    fn percent_encode_query_value_keeps_unreserved_only() {
        // 英数 + `-._~` だけがそのまま残ることを保証する。
        let encoded = percent_encode_query_value("AZaz09-._~");
        assert_eq!(encoded, "AZaz09-._~");
    }

    #[test]
    fn default_oembed_img_alt_is_gyazo_image() {
        // 回帰テスト: alt 引数が省略されたときに img_tag_html の alt に
        // 入るデフォルト値が "Gyazo image" であることを保証する。
        // ツール本体 (gyazo_get_oembed_metadata) が DEFAULT_OEMBED_IMG_ALT を
        // 参照しているので、この定数を変えるとデフォルト alt も変わる。
        assert_eq!(DEFAULT_OEMBED_IMG_ALT, "Gyazo image");

        // デフォルト値で組み立てた img タグが期待どおりになることも確認する。
        let html = build_oembed_img_tag(
            "https://i.gyazo.com/abc.png",
            10,
            20,
            DEFAULT_OEMBED_IMG_ALT,
        );
        assert!(
            html.contains("alt=\"Gyazo image\""),
            "デフォルト alt が img タグに反映されていません: {html}"
        );
    }

    #[test]
    fn build_oembed_discovery_link_matches_oembed_spec_form() {
        // oEmbed spec 第 4 章で定義された discovery link 形式
        // (`<link rel="alternate" type="application/json+oembed" href="..." />`)
        // を生成していることを保証する。Gyazo docs の例とも整合する。
        let link = build_oembed_discovery_link("https://gyazo.com/abc123");
        assert_eq!(
            link,
            "<link rel=\"alternate\" type=\"application/json+oembed\" \
             href=\"https://api.gyazo.com/api/oembed?url=https%3A%2F%2Fgyazo.com%2Fabc123\" \
             title=\"Image shared with Gyazo\" />"
        );
    }

    #[test]
    fn build_oembed_discovery_link_percent_encodes_url_query_param() {
        // image_page_url に `&` 等が含まれていても、href の query 値として
        // 安全に埋め込まれること (& が %26 に変換され、HTML 属性脱出も
        // 起こさないこと) を保証する。
        let link = build_oembed_discovery_link("https://gyazo.com/abc?x=1&y=2");
        assert!(
            link.contains("href=\"https://api.gyazo.com/api/oembed?url=https%3A%2F%2Fgyazo.com%2Fabc%3Fx%3D1%26y%3D2\""),
            "{link}"
        );
        // percent-encode 後は HTML 特殊文字が残らないので、`&amp;` への
        // HTML エスケープは不要 (= 出力に `&amp;` も生の `&` も無い)
        assert!(!link.contains("&amp;"), "{link}");
        assert!(!link.contains("=1&y"), "{link}");
    }
}