ryoshimu commited on
Commit
48e54a4
·
1 Parent(s): 8b05932
Files changed (3) hide show
  1. README1.md +77 -0
  2. app.py +82 -0
  3. requirements.txt +1 -0
README1.md ADDED
@@ -0,0 +1,77 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # シンプルなMCPサーバー
2
+
3
+ このSpaceは、Model Context Protocol (MCP) サーバーとして機能するシンプルなGradioアプリです。
4
+
5
+ ## 提供される機能
6
+
7
+ 1. **テキスト反転**: テキストを逆順にします
8
+ 2. **文字数カウント**: テキストの文字数を数えます
9
+ 3. **大文字/小文字変換**: テキストを大文字または小文字に変換します
10
+
11
+ ## Claudeへの接続方法
12
+
13
+ ### 1. Spaceをデプロイ
14
+
15
+ 1. [Hugging Face](https://huggingface.co/new-space)で新しいSpaceを作成
16
+ 2. SDKに「Gradio」を選択
17
+ 3. 上記の3つのファイル(`app.py`, `requirements.txt`, `README.md`)をアップロード
18
+ 4. Spaceが起動するまで待つ
19
+
20
+ ### 2. ClaudeにMCPサーバーを追加
21
+
22
+ Spaceが起動したら、以下の設定をClaude Desktop(または他のMCPクライアント)の設定に追加します:
23
+
24
+ **設定ファイルの場所:**
25
+ - macOS: `~/Library/Application Support/Claude/claude_desktop_config.json`
26
+ - Windows: `%APPDATA%\Claude\claude_desktop_config.json`
27
+
28
+ **追加する設定:**
29
+ ```json
30
+ {
31
+ "mcpServers": {
32
+ "my-simple-mcp": {
33
+ "url": "https://YOUR-USERNAME-SPACE-NAME.hf.space/gradio_api/mcp/sse"
34
+ }
35
+ }
36
+ }
37
+ ```
38
+
39
+ `YOUR-USERNAME-SPACE-NAME`は実際のSpace URLに置き換えてください。
40
+
41
+ ### 3. プライベートSpaceの場合
42
+
43
+ プライベートSpaceを使用する場合は、Hugging Face トークンを追加します:
44
+
45
+ ```json
46
+ {
47
+ "mcpServers": {
48
+ "my-simple-mcp": {
49
+ "url": "https://YOUR-USERNAME-SPACE-NAME.hf.space/gradio_api/mcp/sse",
50
+ "headers": {
51
+ "Authorization": "Bearer YOUR-HF-TOKEN"
52
+ }
53
+ }
54
+ }
55
+ }
56
+ ```
57
+
58
+ ### 4. 動作確認
59
+
60
+ Claude Desktopを再起動し、以下のように質問してみてください:
61
+
62
+ - "Hello Worldを逆にして"
63
+ - "おはようございますは何文字?"
64
+ - "hello worldを大文字にして"
65
+
66
+ Claudeが自動的にMCPサーバーのツールを使用して回答します!
67
+
68
+ ## MCPエンドポイントの確認
69
+
70
+ Spaceのフッターにある「View API」リンクをクリックし、「MCP」タブを選択すると、正確な接続情報が表示されます。
71
+
72
+ ## トラブルシューティング
73
+
74
+ - Spaceが正しく起動していることを確認
75
+ - URLが正しいことを確認(特に`/gradio_api/mcp/sse`の部分)
76
+ - Claude Desktopを再起動
77
+ - 設定ファイルのJSONが正しいか確認
app.py ADDED
@@ -0,0 +1,82 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+
3
+ def reverse_text(text: str) -> str:
4
+ """
5
+ テキストを逆順にします。
6
+
7
+ Args:
8
+ text: 逆順にしたいテキスト
9
+
10
+ Returns:
11
+ 逆順になったテキスト
12
+ """
13
+ return text[::-1]
14
+
15
+ def count_characters(text: str) -> int:
16
+ """
17
+ テキストの文字数を数えます。
18
+
19
+ Args:
20
+ text: 文字数を数えたいテキスト
21
+
22
+ Returns:
23
+ 文字数
24
+ """
25
+ return len(text)
26
+
27
+ def convert_case(text: str, case_type: str) -> str:
28
+ """
29
+ テキストを大文字または小文字に変換します。
30
+
31
+ Args:
32
+ text: 変換したいテキスト
33
+ case_type: 変換タイプ ("upper" または "lower")
34
+
35
+ Returns:
36
+ 変換されたテキスト
37
+ """
38
+ if case_type == "upper":
39
+ return text.upper()
40
+ elif case_type == "lower":
41
+ return text.lower()
42
+ else:
43
+ return "エラー: case_typeは 'upper' または 'lower' を指定してください"
44
+
45
+ # 各関数用のInterfaceを作成
46
+ reverse_interface = gr.Interface(
47
+ fn=reverse_text,
48
+ inputs=gr.Textbox(label="テキスト", value="Hello World"),
49
+ outputs=gr.Textbox(label="結果"),
50
+ title="テキスト反転",
51
+ description="入力したテキストを逆順にします"
52
+ )
53
+
54
+ count_interface = gr.Interface(
55
+ fn=count_characters,
56
+ inputs=gr.Textbox(label="テキスト", value="Hello World"),
57
+ outputs=gr.Number(label="文字数"),
58
+ title="文字数カウント",
59
+ description="入力したテキストの文字数を数えます"
60
+ )
61
+
62
+ case_interface = gr.Interface(
63
+ fn=convert_case,
64
+ inputs=[
65
+ gr.Textbox(label="テキスト", value="Hello World"),
66
+ gr.Radio(["upper", "lower"], label="変換タイプ", value="upper")
67
+ ],
68
+ outputs=gr.Textbox(label="結果"),
69
+ title="大文字/小文字変換",
70
+ description="テキストを大文字または小文字に変換します"
71
+ )
72
+
73
+ # TabbedInterfaceで複数の機能をまとめる
74
+ demo = gr.TabbedInterface(
75
+ [reverse_interface, count_interface, case_interface],
76
+ ["テキスト反転", "文字数カウント", "大文字/小文字変換"],
77
+ title="シンプルなMCPサーバー"
78
+ )
79
+
80
+ if __name__ == "__main__":
81
+ # mcp_server=Trueを設定してMCPサーバーとして起動
82
+ demo.launch(mcp_server=True)
requirements.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ gradio[mcp]