Model Context Protocol (MCP) Giriş
James Chapman
AI Curriculum Manager, DataCamp
Bölüm 2

Bölüm 2
Kaynaklar: MCP istemcisinin aldığı salt-okunur veriler veya veri nesneleri


from mcp.server.fastmcp import FastMCP mcp = FastMCP("Timezone Converter")@mcp.resource("file://locations.txt")def get_locations(): try: with open('locations.txt', 'r') as f: content = f.read() return content except FileNotFoundError: return "locations.txt file not found"
URI: tekdüzen kaynak tanımlayıcısı
locations.txt (sunucu tarafı) → saat dilimi konumlarının listesi
@mcp.resource("file://locations.txt")
def get_locations() -> str:
"""
Saat dilimi dönüşümü için şehir listesini alın.
Returns:
Şehir adlarını içeren locations.txt dosyasının içeriği
"""
try:
with open('locations.txt', 'r') as f:
content = f.read()
return content
except FileNotFoundError:
return "locations.txt file not found"
from mcp.server.fastmcp import FastMCP
mcp = FastMCP("Timezone Converter")
# Önceki araçlar...
@mcp.resource("file://locations.txt")
def get_locations() -> str:
# ...
if __name__ == "__main__":
mcp.run(transport="stdio")
async def list_resources(): """MCP sunucusundaki tüm mevcut kaynakları listeleyin.""" params = StdioServerParameters(command=sys.executable, args=["timezone_server.py"]) async with stdio_client(params) as (reader, writer): async with ClientSession(reader, writer) as session: await session.initialize()response = await session.list_resources()print("Mevcut kaynaklar:") for resource in response.resources: print(f" - {resource.uri}") print(f" Ad: {resource.name}") print(f" Açıklama: {resource.description}") return response.resources
print(asyncio.run(list_resources()))
Mevcut kaynaklar:
- file://locations.txt/
Ad: get_locations
Açıklama:
Saat dilimi dönüşümü için şehir listesini alın.
Returns:
Şehir adlarını içeren locations.txt dosyasının içeriği
[Resource(name='get_locations', title=None, uri=AnyUrl('file://locations.txt/'),
description='\n Saat dilimi dönüşümü için şehir listesini alın.\n\n...',
mimeType='text/plain', size=None, icons=None, annotations=None, meta=None)]
async def read_resource(resource_uri: str): """URI ile belirli bir kaynağı okuyun.""" params = StdioServerParameters(command=sys.executable, args=["timezone_server.py"]) async with stdio_client(params) as (reader, writer): async with ClientSession(reader, writer) as session: await session.initialize()print(f"Kaynak okunuyor: {resource_uri}") resource_content = await session.read_resource(resource_uri)for content in resource_content.contents: print(f"\nİçerik ({content.mimeType}):") print(content.text) return resource_content
print(asyncio.run(read_resource('file://locations.txt/')))
Kaynak okunuyor: file://locations.txt
İçerik (text/plain):
Africa/Abidjan
Africa/Accra
Africa/Addis_Ababa
Africa/Algiers
...
ReadResourceResult(meta=None, contents=[TextResourceContents(uri=AnyUrl('file://locations.txt/'),
mimeType='text/plain', meta=None, text='Africa/Abidjan\nAfrica/Accra...')])
Model Context Protocol (MCP) Giriş