File size: 2,773 Bytes
317ab4b
cb04ff5
 
317ab4b
 
 
 
 
 
 
 
 
 
ad95a4d
cb04ff5
317ab4b
 
0bbd02b
 
317ab4b
0bbd02b
 
317ab4b
0bbd02b
317ab4b
0bbd02b
317ab4b
 
 
 
 
 
 
 
 
 
0bbd02b
317ab4b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ad95a4d
317ab4b
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
"""Gradio MCP server implementation for Hugging Face Spaces."""
import gradio as gr

# MCP対応のツール関数を定義(型ヒントとdocstringが必須)
def echo(text: str) -> str:
    """Return the provided text unchanged.
    
    Args:
        text: The text to echo back
        
    Returns:
        The same text that was provided
    """
    return text

def reverse_text(text: str) -> str:
    """Reverse the provided text.
    
    Args:
        text: The text to reverse
        
    Returns:
        The reversed text
    """
    return text[::-1]

def count_words(text: str) -> int:
    """Count the number of words in the text.
    
    Args:
        text: The text to count words in
        
    Returns:
        The number of words in the text
    """
    return len(text.split())

# Gradioインターフェースを作成
with gr.Blocks(title="MCP Sample Server", theme=gr.themes.Soft()) as demo:
    gr.Markdown(
        """
        # MCP Sample Server
        This Space provides MCP-compatible tools that can be used with Claude.ai and other MCP clients.
        
        ## Available Tools:
        - **echo**: Returns the provided text unchanged
        - **reverse_text**: Reverses the provided text
        - **count_words**: Counts the number of words in the text
        
        ## How to connect:
        1. Go to https://huggingface.co/settings/mcp
        2. Set up your MCP client (Claude.ai)
        3. Click the MCP badge on this Space and select "Add to MCP tools"
        4. The tools will be available in your MCP client
        """
    )
    
    with gr.Tab("Echo Tool"):
        with gr.Row():
            echo_input = gr.Textbox(label="Input Text", placeholder="Enter text to echo")
            echo_output = gr.Textbox(label="Output", interactive=False)
        echo_btn = gr.Button("Echo")
        echo_btn.click(echo, inputs=echo_input, outputs=echo_output)
    
    with gr.Tab("Reverse Tool"):
        with gr.Row():
            reverse_input = gr.Textbox(label="Input Text", placeholder="Enter text to reverse")
            reverse_output = gr.Textbox(label="Reversed Text", interactive=False)
        reverse_btn = gr.Button("Reverse")
        reverse_btn.click(reverse_text, inputs=reverse_input, outputs=reverse_output)
    
    with gr.Tab("Word Count Tool"):
        with gr.Row():
            count_input = gr.Textbox(label="Input Text", placeholder="Enter text to count words")
            count_output = gr.Number(label="Word Count", interactive=False)
        count_btn = gr.Button("Count Words")
        count_btn.click(count_words, inputs=count_input, outputs=count_output)

# 重要: mcp_server=True を指定してMCPサーバーとして公開
if __name__ == "__main__":
    demo.launch(mcp_server=True)