FastMCP: Membangun Aplikasi AI Terstandarisasi dengan Model Context Protocol

Subscribe dengan Account Google untuk mendapatkan News Letter terbaru dari Halovina !
FastMCP: Membangun Aplikasi AI Terstandarisasi dengan Model Context Protocol

Contoh Komponen Inti Client-Server MCP dengan FastMCP


FastMCP mempermudah pendefinisian komponen-komponen inti server MCP menggunakan dekorator Python yang intuitif. Berikut adalah contoh komponen utamanya:

 

(1) Tools 


Tools adalah fungsi yang dapat dieksekusi oleh LLM melalui server. Ini memungkinkan LLM untuk melakukan tindakan di dunia nyata, seperti mengirim email, mencari data dari API, atau memanipulasi file.

Example client and server

server.py

from fastmcp import FastMCP

# 1. Create the server
mcp = FastMCP(name="My First MCP Server")

# 2. Add a tool
@mcp.tool
def add(a: int, b: int) -> int:
"""Adds two integer numbers together."""
return a + b
# 3. Make the server runnable
if __name__ == "__main__":
mcp.run()

Note : use this command to run (python server.py)

client.py

import asyncio
from fastmcp import Client, FastMCP

# Local Python script
client = Client("server.py")

async def main():
async with client:
# Basic server interaction
await client.ping()

# Execute operations
result = await client.call_tool("add", {"a": 1,"b": 2})
print(result.data)

asyncio.run(main())

Note : open new terminal and use this command to run (python client.py)

(2) Resources & Templates


Resources adalah data atau konten terstruktur yang dapat diakses oleh LLM untuk mendapatkan konteks. Ini bisa berupa dokumen, entri database, atau file konfigurasi.


Example client and server


server.py


from fastmcp import FastMCP

# 1. Create the server
mcp = FastMCP(name="My First MCP Server")

# 2. Add a static resource
@mcp.resource("resource://config")
def get_config() -> dict:
"""Provides the application's configuration."""
return {"version": "1.0", "author": "MyTeam"}

# 3. Make the server runnable
if __name__ == "__main__":
mcp.run()



Note : use this command to run (python server.py)


client.py


import asyncio
from fastmcp import Client

# Local Python script
client = Client("server.py")

async def main():
async with client:
# Basic server interaction
await client.ping()

result = await client.read_resource("resource://config")
print(result[0].text)

asyncio.run(main())



Note : open new terminal and use this command to run (python client.py)

(3) Prompts


Prompts adalah templat perintah yang dapat digunakan kembali untuk tugas-tugas umum. Ini membantu dalam menyusun respons LLM yang terstruktur dan konsisten.

Example client and server


server.py


from fastmcp import FastMCP

# 1. Create the server
mcp = FastMCP(name="My First MCP Server")

# 2. Add a prompt
@mcp.prompt
def analyze_data(data_points: list[float]) -> str:
"""Creates a prompt asking for analysis of numerical data."""
formatted_data = ", ".join(str(point) for point in data_points)
return f"Please analyze these data points: {formatted_data}"

# 3. Make the server runnable
if __name__ == "__main__":
mcp.run()



Note : use this command to run (python server.py)


client.py


import asyncio
from fastmcp import Client

# Local Python script
client = Client("server.py")

async def main():
async with client:
# Basic server interaction
await client.ping()

result = await client.get_prompt("analyze_data", {
"data_points": [3.2, 4.5, 5.1],
})
print(result.messages[0].content.text)

asyncio.run(main())



Note : open new terminal and use this command to run (python client.py)

Baca artikel lainya :