Hannan2004 commited on
Commit
624d181
·
verified ·
1 Parent(s): 9f8efef

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -49
app.py CHANGED
@@ -1,68 +1,71 @@
1
  from mcp.server.fastmcp import FastMCP
2
  import gradio as gr
3
 
4
- mcp = FastMCP("My First MCP server")
 
5
 
 
6
  @mcp.tool()
7
  def get_weather(location: str) -> str:
8
  """
9
  Get the current weather for a given location.
10
-
11
  Args:
12
  location (str): The name of the location to get the weather for.
13
-
14
  Returns:
15
  str: The current weather information for the specified location.
16
  """
17
- return f"The weather for the given {location} : Sunny, 72 Degree Fahrenheit"
18
-
19
- @mcp.tool()
20
- def add_two_numbers(a: int, b: int) -> int:
21
- """
22
- Adds two integers
23
-
24
- Args:
25
- a (int): The first number
26
- b (int): The second number
27
-
28
- Returns:
29
- int: The sum of two numbers
30
- """
31
- return a + b
32
 
33
  @mcp.resource("weather://{location}")
34
  def get_weather_resource(location: str) -> str:
35
  """
36
- Resource to get current weather for the given location
37
-
38
  Args:
39
  location (str): The name of the location to get the weather for.
40
-
41
  Returns:
42
- str: The weather resource string containing the weather information.
43
  """
44
- return f"The weather for the given {location}: Sunny, 72 degree Fahrenheit."
45
 
46
  @mcp.prompt()
47
- def get_weather_prompt(location: str) -> str:
48
  """
49
- Create a weather report prompt
50
-
51
- Args:
52
- location (str): The name of the location to get the weather for.
53
-
54
  Returns:
55
  str: A formatted prompt string to instruct the model to act as a weather expert.
56
  """
57
- return "You are a weather expert and you are supposed to provide the weather report"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58
 
 
59
  def weather_ui(location: str) -> str:
60
  """
61
  Wrapper for weather UI to fetch weather.
62
-
63
- Args:
64
- location (str): location name.
65
-
66
  Returns:
67
  str: Weather info string.
68
  """
@@ -71,31 +74,32 @@ def weather_ui(location: str) -> str:
71
  def math_ui(a: int, b: int) -> int:
72
  """
73
  Wrapper for math UI to add numbers.
74
-
75
  Args:
76
- a (int): The first number.
77
- b (int): The second number.
78
-
79
  Returns:
80
- int: The sum of two numbers
81
  """
82
  return add_two_numbers(a, b)
83
 
84
  with gr.Blocks(title="My first MCP server") as demo:
85
  gr.Markdown("## Weather + Math MCP Server")
86
 
87
- with gr.tab("Weather Service"):
88
- location_input = gr.Textbox(label="Enter location")
89
  weather_output = gr.Textbox(label="Weather Result")
90
- weather_btn = gr.Button("Get weather")
91
  weather_btn.click(fn=weather_ui, inputs=location_input, outputs=weather_output)
92
 
93
- with gr.tab("Math Service"):
94
- num_1_input = gr.Number(label="Enter first number")
95
- num_2_input = gr.Number(label="Enter second number")
96
- output = gr.Number(label="Sum")
97
- math_btn = gr.Button("Add numbers")
98
- math_btn.click(fn=math_ui, inputs=[num_1_input, num_2_input], outputs=output)
99
-
 
100
  if __name__ == "__main__":
101
  demo.launch(mcp_server=True)
 
1
  from mcp.server.fastmcp import FastMCP
2
  import gradio as gr
3
 
4
+ # Create a MCP Server
5
+ mcp = FastMCP("My first MCP server")
6
 
7
+ # Defining weather tools
8
  @mcp.tool()
9
  def get_weather(location: str) -> str:
10
  """
11
  Get the current weather for a given location.
12
+
13
  Args:
14
  location (str): The name of the location to get the weather for.
15
+
16
  Returns:
17
  str: The current weather information for the specified location.
18
  """
19
+ return f"The weather for the given {location} : Sunny, 72 F"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
 
21
  @mcp.resource("weather://{location}")
22
  def get_weather_resource(location: str) -> str:
23
  """
24
+ Resource to get the current weather for the given location.
25
+
26
  Args:
27
  location (str): The name of the location to get the weather for.
28
+
29
  Returns:
30
+ str: The weather resource string containing weather information.
31
  """
32
+ return f"The weather for the given {location} : Sunny, 72 F"
33
 
34
  @mcp.prompt()
35
+ def weather_prompt(location: str) -> str:
36
  """
37
+ Create a weather report prompt.
38
+
39
+ Args:
40
+ location (str): The name of the location for which the weather report prompt should be created.
41
+
42
  Returns:
43
  str: A formatted prompt string to instruct the model to act as a weather expert.
44
  """
45
+ return f"You are a weather expert and you are supposed to provide weather information for the given location {location}."
46
+
47
+ @mcp.tool()
48
+ def add_two_numbers(a: int, b: int) -> int:
49
+ """
50
+ Add two numbers.
51
+
52
+ Args:
53
+ a (int): The first number to be added.
54
+ b (int): The second number to be added.
55
+
56
+ Returns:
57
+ int: The sum of the two numbers.
58
+ """
59
+ return a + b
60
 
61
+ # Here will be defining the Gradio UI
62
  def weather_ui(location: str) -> str:
63
  """
64
  Wrapper for weather UI to fetch weather.
65
+
66
+ Args:
67
+ location (str): Location name.
68
+
69
  Returns:
70
  str: Weather info string.
71
  """
 
74
  def math_ui(a: int, b: int) -> int:
75
  """
76
  Wrapper for math UI to add numbers.
77
+
78
  Args:
79
+ a (int): First number.
80
+ b (int): Second number.
81
+
82
  Returns:
83
+ int: Sum of the two numbers.
84
  """
85
  return add_two_numbers(a, b)
86
 
87
  with gr.Blocks(title="My first MCP server") as demo:
88
  gr.Markdown("## Weather + Math MCP Server")
89
 
90
+ with gr.Tab("Weather Service"):
91
+ location_input = gr.Textbox(label="Enter Location")
92
  weather_output = gr.Textbox(label="Weather Result")
93
+ weather_btn = gr.Button("Get Weather")
94
  weather_btn.click(fn=weather_ui, inputs=location_input, outputs=weather_output)
95
 
96
+ with gr.Tab("Math Service"):
97
+ num_1_input = gr.Number(label="Enter First Number")
98
+ num_2_input = gr.Number(label="Enter Second Number")
99
+ math_output = gr.Number(label="Sum")
100
+ math_btn = gr.Button("Add Numbers")
101
+ math_btn.click(fn=math_ui, inputs=[num_1_input, num_2_input], outputs=math_output)
102
+
103
+ # Running the MCP server
104
  if __name__ == "__main__":
105
  demo.launch(mcp_server=True)