Create server.py
Browse files
server.py
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Import Hugging Face datasets
|
| 2 |
+
from datasets import load_dataset
|
| 3 |
+
from colorama import Fore
|
| 4 |
+
from mcp.server.fastmcp import FastMCP
|
| 5 |
+
import chromadb
|
| 6 |
+
|
| 7 |
+
# Create server
|
| 8 |
+
mcp = FastMCP("croptimizeserver")
|
| 9 |
+
|
| 10 |
+
# Load crop optimization dataset
|
| 11 |
+
dataset = load_dataset("DARJYO/sawotiQ29_crop_optimization")
|
| 12 |
+
|
| 13 |
+
# Initialize ChromaDB for crop data
|
| 14 |
+
chroma_client = chromadb.PersistentClient(path="crop_db")
|
| 15 |
+
collection = chroma_client.get_collection(name="crop_data")
|
| 16 |
+
|
| 17 |
+
# Add prompt function for crop recommendations
|
| 18 |
+
@mcp.prompt()
|
| 19 |
+
def crop_recommendation(crop_data: str) -> str:
|
| 20 |
+
"""Prompt template for generating crop recommendations"""
|
| 21 |
+
return f"""You are an agricultural expert assistant designed to provide crop optimization advice.
|
| 22 |
+
Analyze the following crop data and provide recommendations for optimal cultivation:
|
| 23 |
+
{crop_data}"""
|
| 24 |
+
|
| 25 |
+
# Resource for searching crop information
|
| 26 |
+
@mcp.resource("crops://search/{query}")
|
| 27 |
+
def search_crops(query: str) -> str:
|
| 28 |
+
"""Search for crops based on growing conditions or characteristics"""
|
| 29 |
+
results = collection.query(
|
| 30 |
+
query_texts=[query],
|
| 31 |
+
n_results=3,
|
| 32 |
+
include=["documents", "metadatas"]
|
| 33 |
+
)
|
| 34 |
+
return str(results)
|
| 35 |
+
|
| 36 |
+
# Tool for getting crop details
|
| 37 |
+
@mcp.tool()
|
| 38 |
+
def crop_details(crop_name: str) -> str:
|
| 39 |
+
"""Get detailed information about a specific crop"""
|
| 40 |
+
filtered_data = dataset['train'].filter(lambda x: x['crop_name'].lower() == crop_name.lower())
|
| 41 |
+
if not filtered_data:
|
| 42 |
+
return f"No information found for {crop_name}"
|
| 43 |
+
return str(filtered_data[0])
|
| 44 |
+
|
| 45 |
+
# Tool for optimal growing conditions
|
| 46 |
+
@mcp.tool()
|
| 47 |
+
def optimal_conditions(crop_name: str) -> str:
|
| 48 |
+
"""Get optimal growing conditions for a specific crop"""
|
| 49 |
+
crop_data = dataset['train'].filter(lambda x: x['crop_name'].lower() == crop_name.lower())
|
| 50 |
+
if not crop_data:
|
| 51 |
+
return f"No data available for {crop_name}"
|
| 52 |
+
|
| 53 |
+
conditions = {
|
| 54 |
+
'temperature': crop_data[0]['optimal_temperature'],
|
| 55 |
+
'rainfall': crop_data[0]['annual_rainfall'],
|
| 56 |
+
'soil_type': crop_data[0]['preferred_soil'],
|
| 57 |
+
'altitude': crop_data[0]['optimal_altitude']
|
| 58 |
+
}
|
| 59 |
+
return str(conditions)
|
| 60 |
+
|
| 61 |
+
# Tool for yield prediction
|
| 62 |
+
@mcp.tool()
|
| 63 |
+
def yield_prediction(crop_name: str, region: str) -> str:
|
| 64 |
+
"""Predict yield for a crop in a specific region"""
|
| 65 |
+
region_data = dataset['train'].filter(lambda x:
|
| 66 |
+
(x['region'].lower() == region.lower()) and
|
| 67 |
+
(x['crop_name'].lower() == crop_name.lower())
|
| 68 |
+
)
|
| 69 |
+
|
| 70 |
+
if not region_data:
|
| 71 |
+
return f"No yield data available for {crop_name} in {region}"
|
| 72 |
+
|
| 73 |
+
prediction = {
|
| 74 |
+
'crop': crop_name,
|
| 75 |
+
'region': region,
|
| 76 |
+
'expected_yield': region_data[0]['average_yield'],
|
| 77 |
+
'optimal_season': region_data[0]['best_season']
|
| 78 |
+
}
|
| 79 |
+
return str(prediction)
|
| 80 |
+
|
| 81 |
+
# Tool for pest/disease information
|
| 82 |
+
@mcp.tool()
|
| 83 |
+
def crop_protection(crop_name: str) -> str:
|
| 84 |
+
"""Get common pests and diseases for a crop"""
|
| 85 |
+
crop_data = dataset['train'].filter(lambda x: x['crop_name'].lower() == crop_name.lower())
|
| 86 |
+
if not crop_data:
|
| 87 |
+
return f"No protection data available for {crop_name}"
|
| 88 |
+
|
| 89 |
+
protection_info = {
|
| 90 |
+
'common_pests': crop_data[0]['common_pests'],
|
| 91 |
+
'common_diseases': crop_data[0]['common_diseases'],
|
| 92 |
+
'prevention_methods': crop_data[0]['prevention_methods']
|
| 93 |
+
}
|
| 94 |
+
return str(protection_info)
|
| 95 |
+
|
| 96 |
+
if __name__ == "__main__":
|
| 97 |
+
mcp.run(transport="stdio")
|