MCP_sample / app.py
ryoshimu
commit
317ab4b
"""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)