akhaliq HF Staff commited on
Commit
c858eda
·
1 Parent(s): e68be88

fix mcp issue

Browse files
Files changed (1) hide show
  1. app.py +233 -0
app.py CHANGED
@@ -769,6 +769,118 @@ def generate(prompt):
769
  - FlashAttention-3 works on H200 hardware via kernels library
770
  - Dynamic shapes add flexibility for variable input sizes
771
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
772
  ## Complete Gradio API Reference
773
 
774
  This reference is automatically synced from https://www.gradio.app/llms.txt to ensure accuracy.
@@ -1094,6 +1206,118 @@ def generate(prompt):
1094
  - FlashAttention-3 works on H200 hardware via kernels library
1095
  - Dynamic shapes add flexibility for variable input sizes
1096
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1097
  ## Complete Gradio API Reference
1098
 
1099
  This reference is automatically synced from https://www.gradio.app/llms.txt to ensure accuracy.
@@ -1850,6 +2074,15 @@ You MUST use this exact format with file separators. DO NOT deviate from this fo
1850
  - Preserve all performance optimizations
1851
  - Add ZeroGPU decorators for new GPU-dependent functions
1852
 
 
 
 
 
 
 
 
 
 
1853
  IMPORTANT: Always ensure "Built with anycoder" appears as clickable text in the header/top section linking to https://huggingface.co/spaces/akhaliq/anycoder - if it's missing from the existing code, add it; if it exists, preserve it.
1854
 
1855
  CRITICAL: For imported spaces that lack anycoder attribution, you MUST add it as part of your modifications. Add it to the header/navigation area as clickable text linking to https://huggingface.co/spaces/akhaliq/anycoder"""
 
769
  - FlashAttention-3 works on H200 hardware via kernels library
770
  - Dynamic shapes add flexibility for variable input sizes
771
 
772
+ ## MCP Server Integration
773
+
774
+ When the user requests an MCP-enabled Gradio app or asks for tool calling capabilities, you MUST enable MCP server functionality.
775
+
776
+ **🚨 CRITICAL: Enabling MCP Server**
777
+ To make your Gradio app function as an MCP (Model Control Protocol) server:
778
+ 1. Set `mcp_server=True` in the `.launch()` method
779
+ 2. Add `"gradio[mcp]"` to requirements.txt (not just `gradio`)
780
+ 3. Ensure all functions have detailed docstrings with proper Args sections
781
+ 4. Use type hints for all function parameters
782
+
783
+ **Example:**
784
+ ```python
785
+ import gradio as gr
786
+
787
+ def letter_counter(word: str, letter: str) -> int:
788
+ \"\"\"
789
+ Count the number of occurrences of a letter in a word or text.
790
+
791
+ Args:
792
+ word (str): The input text to search through
793
+ letter (str): The letter to search for
794
+
795
+ Returns:
796
+ int: The number of times the letter appears
797
+ \"\"\"
798
+ return word.lower().count(letter.lower())
799
+
800
+ demo = gr.Interface(
801
+ fn=letter_counter,
802
+ inputs=[gr.Textbox("strawberry"), gr.Textbox("r")],
803
+ outputs=[gr.Number()],
804
+ title="Letter Counter",
805
+ description="Count letter occurrences in text."
806
+ )
807
+
808
+ if __name__ == "__main__":
809
+ demo.launch(mcp_server=True)
810
+ ```
811
+
812
+ **When to Enable MCP:**
813
+ - User explicitly requests "MCP server" or "MCP-enabled app"
814
+ - User wants tool calling capabilities for LLMs
815
+ - User mentions Claude Desktop, Cursor, or Cline integration
816
+ - User wants to expose functions as tools for AI assistants
817
+
818
+ **MCP Requirements:**
819
+ 1. **Dependencies:** Always use `gradio[mcp]` in requirements.txt (not plain `gradio`)
820
+ 2. **Docstrings:** Every function must have a detailed docstring with:
821
+ - Brief description on first line
822
+ - Args section listing each parameter with type and description
823
+ - Returns section (optional but recommended)
824
+ 3. **Type Hints:** All parameters must have type hints (e.g., `word: str`, `count: int`)
825
+ 4. **Default Values:** Use default values in components to provide examples
826
+
827
+ **Best Practices for MCP Tools:**
828
+ - Use descriptive function names (they become tool names)
829
+ - Keep functions focused and single-purpose
830
+ - Accept string parameters when possible for better compatibility
831
+ - Return simple types (str, int, float, list, dict) rather than complex objects
832
+ - Use gr.Header for authentication headers when needed
833
+ - Use gr.Progress() for long-running operations
834
+
835
+ **Multiple Tools Example:**
836
+ ```python
837
+ import gradio as gr
838
+
839
+ def add_numbers(a: str, b: str) -> str:
840
+ \"\"\"
841
+ Add two numbers together.
842
+
843
+ Args:
844
+ a (str): First number
845
+ b (str): Second number
846
+
847
+ Returns:
848
+ str: Sum of the two numbers
849
+ \"\"\"
850
+ return str(int(a) + int(b))
851
+
852
+ def multiply_numbers(a: str, b: str) -> str:
853
+ \"\"\"
854
+ Multiply two numbers.
855
+
856
+ Args:
857
+ a (str): First number
858
+ b (str): Second number
859
+
860
+ Returns:
861
+ str: Product of the two numbers
862
+ \"\"\"
863
+ return str(int(a) * int(b))
864
+
865
+ with gr.Blocks() as demo:
866
+ gr.Markdown("# Math Tools MCP Server")
867
+
868
+ with gr.Tab("Add"):
869
+ gr.Interface(add_numbers, [gr.Textbox("5"), gr.Textbox("3")], gr.Textbox())
870
+
871
+ with gr.Tab("Multiply"):
872
+ gr.Interface(multiply_numbers, [gr.Textbox("4"), gr.Textbox("7")], gr.Textbox())
873
+
874
+ if __name__ == "__main__":
875
+ demo.launch(mcp_server=True)
876
+ ```
877
+
878
+ **REMEMBER:** If MCP is requested, ALWAYS:
879
+ 1. Set `mcp_server=True` in `.launch()`
880
+ 2. Use `gradio[mcp]` in requirements.txt
881
+ 3. Include complete docstrings with Args sections
882
+ 4. Add type hints to all parameters
883
+
884
  ## Complete Gradio API Reference
885
 
886
  This reference is automatically synced from https://www.gradio.app/llms.txt to ensure accuracy.
 
1206
  - FlashAttention-3 works on H200 hardware via kernels library
1207
  - Dynamic shapes add flexibility for variable input sizes
1208
 
1209
+ ## MCP Server Integration
1210
+
1211
+ When the user requests an MCP-enabled Gradio app or asks for tool calling capabilities, you MUST enable MCP server functionality.
1212
+
1213
+ **🚨 CRITICAL: Enabling MCP Server**
1214
+ To make your Gradio app function as an MCP (Model Control Protocol) server:
1215
+ 1. Set `mcp_server=True` in the `.launch()` method
1216
+ 2. Add `"gradio[mcp]"` to requirements.txt (not just `gradio`)
1217
+ 3. Ensure all functions have detailed docstrings with proper Args sections
1218
+ 4. Use type hints for all function parameters
1219
+
1220
+ **Example:**
1221
+ ```python
1222
+ import gradio as gr
1223
+
1224
+ def letter_counter(word: str, letter: str) -> int:
1225
+ \"\"\"
1226
+ Count the number of occurrences of a letter in a word or text.
1227
+
1228
+ Args:
1229
+ word (str): The input text to search through
1230
+ letter (str): The letter to search for
1231
+
1232
+ Returns:
1233
+ int: The number of times the letter appears
1234
+ \"\"\"
1235
+ return word.lower().count(letter.lower())
1236
+
1237
+ demo = gr.Interface(
1238
+ fn=letter_counter,
1239
+ inputs=[gr.Textbox("strawberry"), gr.Textbox("r")],
1240
+ outputs=[gr.Number()],
1241
+ title="Letter Counter",
1242
+ description="Count letter occurrences in text."
1243
+ )
1244
+
1245
+ if __name__ == "__main__":
1246
+ demo.launch(mcp_server=True)
1247
+ ```
1248
+
1249
+ **When to Enable MCP:**
1250
+ - User explicitly requests "MCP server" or "MCP-enabled app"
1251
+ - User wants tool calling capabilities for LLMs
1252
+ - User mentions Claude Desktop, Cursor, or Cline integration
1253
+ - User wants to expose functions as tools for AI assistants
1254
+
1255
+ **MCP Requirements:**
1256
+ 1. **Dependencies:** Always use `gradio[mcp]` in requirements.txt (not plain `gradio`)
1257
+ 2. **Docstrings:** Every function must have a detailed docstring with:
1258
+ - Brief description on first line
1259
+ - Args section listing each parameter with type and description
1260
+ - Returns section (optional but recommended)
1261
+ 3. **Type Hints:** All parameters must have type hints (e.g., `word: str`, `count: int`)
1262
+ 4. **Default Values:** Use default values in components to provide examples
1263
+
1264
+ **Best Practices for MCP Tools:**
1265
+ - Use descriptive function names (they become tool names)
1266
+ - Keep functions focused and single-purpose
1267
+ - Accept string parameters when possible for better compatibility
1268
+ - Return simple types (str, int, float, list, dict) rather than complex objects
1269
+ - Use gr.Header for authentication headers when needed
1270
+ - Use gr.Progress() for long-running operations
1271
+
1272
+ **Multiple Tools Example:**
1273
+ ```python
1274
+ import gradio as gr
1275
+
1276
+ def add_numbers(a: str, b: str) -> str:
1277
+ \"\"\"
1278
+ Add two numbers together.
1279
+
1280
+ Args:
1281
+ a (str): First number
1282
+ b (str): Second number
1283
+
1284
+ Returns:
1285
+ str: Sum of the two numbers
1286
+ \"\"\"
1287
+ return str(int(a) + int(b))
1288
+
1289
+ def multiply_numbers(a: str, b: str) -> str:
1290
+ \"\"\"
1291
+ Multiply two numbers.
1292
+
1293
+ Args:
1294
+ a (str): First number
1295
+ b (str): Second number
1296
+
1297
+ Returns:
1298
+ str: Product of the two numbers
1299
+ \"\"\"
1300
+ return str(int(a) * int(b))
1301
+
1302
+ with gr.Blocks() as demo:
1303
+ gr.Markdown("# Math Tools MCP Server")
1304
+
1305
+ with gr.Tab("Add"):
1306
+ gr.Interface(add_numbers, [gr.Textbox("5"), gr.Textbox("3")], gr.Textbox())
1307
+
1308
+ with gr.Tab("Multiply"):
1309
+ gr.Interface(multiply_numbers, [gr.Textbox("4"), gr.Textbox("7")], gr.Textbox())
1310
+
1311
+ if __name__ == "__main__":
1312
+ demo.launch(mcp_server=True)
1313
+ ```
1314
+
1315
+ **REMEMBER:** If MCP is requested, ALWAYS:
1316
+ 1. Set `mcp_server=True` in `.launch()`
1317
+ 2. Use `gradio[mcp]` in requirements.txt
1318
+ 3. Include complete docstrings with Args sections
1319
+ 4. Add type hints to all parameters
1320
+
1321
  ## Complete Gradio API Reference
1322
 
1323
  This reference is automatically synced from https://www.gradio.app/llms.txt to ensure accuracy.
 
2074
  - Preserve all performance optimizations
2075
  - Add ZeroGPU decorators for new GPU-dependent functions
2076
 
2077
+ **MCP Server Support:**
2078
+ - If the user requests MCP functionality or tool calling capabilities:
2079
+ 1. Add `mcp_server=True` to the `.launch()` method if not present
2080
+ 2. Ensure `gradio[mcp]` is in requirements.txt (not just `gradio`)
2081
+ 3. Add detailed docstrings with Args sections to all functions
2082
+ 4. Add type hints to all function parameters
2083
+ - Preserve existing MCP configurations if already present
2084
+ - When adding new tools, follow MCP docstring format with Args and Returns sections
2085
+
2086
  IMPORTANT: Always ensure "Built with anycoder" appears as clickable text in the header/top section linking to https://huggingface.co/spaces/akhaliq/anycoder - if it's missing from the existing code, add it; if it exists, preserve it.
2087
 
2088
  CRITICAL: For imported spaces that lack anycoder attribution, you MUST add it as part of your modifications. Add it to the header/navigation area as clickable text linking to https://huggingface.co/spaces/akhaliq/anycoder"""