Hannan2004 commited on
Commit
e7a0002
·
verified ·
1 Parent(s): 404c4a9

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +105 -0
app.py ADDED
@@ -0,0 +1,105 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ """
72
+ return get_weather(location)
73
+
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)