File size: 3,012 Bytes
717f085
 
 
624d181
 
717f085
624d181
717f085
42dfc3d
9ca3ae2
 
624d181
9ca3ae2
4995f4f
624d181
9ca3ae2
 
 
624d181
717f085
9f8efef
 
9ca3ae2
624d181
 
4995f4f
9f8efef
624d181
4995f4f
624d181
9ca3ae2
624d181
717f085
9f8efef
624d181
42dfc3d
624d181
 
 
 
 
9ca3ae2
9f8efef
42dfc3d
624d181
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42dfc3d
624d181
4995f4f
 
 
624d181
 
 
 
4995f4f
 
 
42dfc3d
 
9ca3ae2
4995f4f
 
624d181
4995f4f
624d181
 
 
4995f4f
624d181
4995f4f
9ca3ae2
 
 
 
717f085
624d181
 
42dfc3d
624d181
42dfc3d
717f085
624d181
 
 
 
 
 
 
 
717f085
9f8efef
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
from mcp.server.fastmcp import FastMCP
import gradio as gr

# Create a MCP Server
mcp = FastMCP("My first MCP server")

# Defining weather tools
@mcp.tool()
def get_weather(location: str) -> str:
    """
    Get the current weather for a given location.
    
    Args:
        location (str): The name of the location to get the weather for.
    
    Returns:
        str: The current weather information for the specified location.
    """
    return f"The weather for the given {location} : Sunny, 72 F"

@mcp.resource("weather://{location}")
def get_weather_resource(location: str) -> str:
    """
    Resource to get the current weather for the given location.
    
    Args:
        location (str): The name of the location to get the weather for.
    
    Returns:
        str: The weather resource string containing weather information.
    """
    return f"The weather for the given {location} : Sunny, 72 F"

@mcp.prompt()
def weather_prompt(location: str) -> str:
    """
    Create a weather report prompt.
    
    Args:
        location (str): The name of the location for which the weather report prompt should be created.
    
    Returns:
        str: A formatted prompt string to instruct the model to act as a weather expert.
    """
    return f"You are a weather expert and you are supposed to provide weather information for the given location {location}."

@mcp.tool()
def add_two_numbers(a: int, b: int) -> int:
    """
    Add two numbers.
    
    Args:
        a (int): The first number to be added.
        b (int): The second number to be added.
    
    Returns:
        int: The sum of the two numbers.
    """
    return a + b

# Here will be defining the Gradio UI
def weather_ui(location: str) -> str:
    """
    Wrapper for weather UI to fetch weather.
    
    Args:
        location (str): Location name.
    
    Returns:
        str: Weather info string.
    """
    return get_weather(location)

def math_ui(a: int, b: int) -> int:
    """
    Wrapper for math UI to add numbers.
    
    Args:
        a (int): First number.
        b (int): Second number.
    
    Returns:
        int: Sum of the two numbers.
    """
    return add_two_numbers(a, b)

with gr.Blocks(title="My first MCP server") as demo:
    gr.Markdown("## Weather + Math MCP Server")

    with gr.Tab("Weather Service"):
        location_input = gr.Textbox(label="Enter Location")
        weather_output = gr.Textbox(label="Weather Result")
        weather_btn = gr.Button("Get Weather")
        weather_btn.click(fn=weather_ui, inputs=location_input, outputs=weather_output)

    with gr.Tab("Math Service"):
        num_1_input = gr.Number(label="Enter First Number")
        num_2_input = gr.Number(label="Enter Second Number")
        math_output = gr.Number(label="Sum")
        math_btn = gr.Button("Add Numbers")
        math_btn.click(fn=math_ui, inputs=[num_1_input, num_2_input], outputs=math_output)
    
# Running the MCP server
if __name__ == "__main__":
    demo.launch(mcp_server=True)