-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.py
More file actions
238 lines (204 loc) · 7.38 KB
/
server.py
File metadata and controls
238 lines (204 loc) · 7.38 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
import os
import logging
import sys
import json
from typing import Optional, List, Dict, Any
from mcp.server.fastmcp import FastMCP
from dotenv import load_dotenv
import rdflib
# Add skills scripts directory to path
# Add skills scripts directory to path (prioritize over installed packages)
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "skills", "wordlift-kg-builder", "scripts"))
# Import new components
from wordlift_client import WordLiftClient
from entity_builder import EntityBuilder
from id_generator import generate_product_id, generate_entity_id
from shacl_validator import SHACLValidator
from markup_validator import MarkupValidator
from kg_sync import KGSyncOrchestrator
from template_configurator import TemplateConfigurator
# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# Load environment variables
load_dotenv()
# Configuration
WORDLIFT_API_KEY = os.getenv("WORDLIFT_API_KEY")
WORDLIFT_BASE_URI = os.getenv("WORDLIFT_BASE_URI")
WORDLIFT_API_ENDPOINT = os.getenv("WORDLIFT_API_ENDPOINT", "https://api.wordlift.io")
# Initialize FastMCP
mcp = FastMCP("WordLift")
def get_client():
"""Returns a WordLift API client."""
if not WORDLIFT_API_KEY:
raise ValueError("WORDLIFT_API_KEY environment variable is not set.")
return WordLiftClient(WORDLIFT_API_KEY, WORDLIFT_API_ENDPOINT)
def get_dataset_uri():
"""Returns the dataset URI from env or raises error."""
if not WORDLIFT_BASE_URI:
raise ValueError("WORDLIFT_BASE_URI environment variable is not set.")
return WORDLIFT_BASE_URI.rstrip('/')
# ==================== KNOWLEDGE GRAPH BUILDING ====================
@mcp.tool()
def import_from_sitemap(sitemap_url: str) -> str:
"""
Import pages from a sitemap.xml using WordLift Sitemap Import API.
Args:
sitemap_url: URL to the sitemap.xml file.
"""
try:
client = get_client()
results = client.import_from_sitemap(sitemap_url)
return f"Successfully imported {len(results)} pages from sitemap: {sitemap_url}"
except Exception as e:
logger.error(f"Sitemap import error: {e}")
return f"Error: {str(e)}"
@mcp.tool()
def import_from_urls(urls: List[str]) -> str:
"""
Import specific URLs into the WordLift Knowledge Graph.
Args:
urls: List of URLs to import.
"""
try:
client = get_client()
results = client.import_from_urls(urls)
return f"Successfully imported {len(results)} URLs"
except Exception as e:
logger.error(f"URL import error: {e}")
return f"Error: {str(e)}"
@mcp.tool()
def build_product(data_json: str) -> str:
"""
Build a JSON-LD Product entity with GS1 Digital Link ID.
Args:
data_json: JSON string containing product data (gtin, name, description, brand, price, currency, etc.).
"""
try:
data = json.loads(data_json)
builder = EntityBuilder(get_dataset_uri())
product = builder.build_product(data)
return json.dumps(product, indent=2)
except Exception as e:
return f"Error: {str(e)}"
@mcp.tool()
def build_organization(data_json: str) -> str:
"""
Build a JSON-LD Organization entity.
Args:
data_json: JSON string containing organization data (name, url, logo, description, etc.).
"""
try:
data = json.loads(data_json)
builder = EntityBuilder(get_dataset_uri())
org = builder.build_organization(data)
return json.dumps(org, indent=2)
except Exception as e:
return f"Error: {str(e)}"
@mcp.tool()
def build_webpage(data_json: str) -> str:
"""
Build a JSON-LD WebPage entity.
Args:
data_json: JSON string containing webpage data (url, name, description, etc.).
"""
try:
data = json.loads(data_json)
builder = EntityBuilder(get_dataset_uri())
webpage = builder.build_webpage(data)
return json.dumps(webpage, indent=2)
except Exception as e:
return f"Error: {str(e)}"
# ==================== VALIDATION & SYNC ====================
@mcp.tool()
def validate_entity(entity_json: str, strict: bool = False) -> str:
"""
Validate a JSON-LD entity against SHACL shapes.
Args:
entity_json: JSON-LD entity string.
strict: Whether to treat recommended fields as required.
"""
try:
entity = json.loads(entity_json)
validator = SHACLValidator()
is_valid, errors, warnings = validator.validate(entity, strict)
result = {
"valid": is_valid,
"errors": errors,
"warnings": warnings
}
return json.dumps(result, indent=2)
except Exception as e:
return f"Error: {str(e)}"
@mcp.tool()
def sync_kg(products_json: str, incremental: bool = False) -> str:
"""
Sync multiple products to the WordLift Knowledge Graph.
Args:
products_json: JSON array of product data.
incremental: Whether to use incremental PATCH updates.
"""
try:
products = json.loads(products_json)
orchestrator = KGSyncOrchestrator(WORDLIFT_API_KEY, get_dataset_uri())
if incremental:
stats = orchestrator.incremental_update(products)
else:
stats = orchestrator.sync_products(products)
return f"Sync complete: {json.dumps(stats, indent=2)}"
except Exception as e:
logger.error(f"Sync error: {e}")
return f"Error: {str(e)}"
# ==================== LEGACY OPERATIONS (Refactored) ====================
@mcp.tool()
def create_or_update_entities(rdf_content: str, content_format: str = "json-ld") -> str:
"""
Creates new entities or updates existing ones in the WordLift Knowledge Graph.
Args:
rdf_content: RDF data as a string.
content_format: Format (turtle, json-ld, xml). Default: json-ld.
"""
try:
client = get_client()
if content_format == "json-ld":
entity = json.loads(rdf_content)
client.create_or_update_entity(entity)
return "Successfully upserted entity in WordLift KG."
else:
# Fallback for Turtle/XML using simple implementation
return "Error: Enhanced client currently optimized for JSON-LD. Use build_product/build_organization for best results."
except Exception as e:
logger.error(f"Upsert error: {e}")
return f"Error: {str(e)}"
@mcp.tool()
def get_entity(entity_id: str) -> str:
"""
Retrieves an entity from the WordLift Knowledge Graph by its ID/IRI.
"""
try:
client = get_client()
entity = client.get_entity_by_url(entity_id)
if entity:
return json.dumps(entity, indent=2)
return f"Entity not found: {entity_id}"
except Exception as e:
logger.error(f"Get entity error: {e}")
return f"Error: {str(e)}"
@mcp.tool()
def delete_entities(entity_ids: str) -> str:
"""
Deletes entities from the WordLift Knowledge Graph by their IDs.
Args:
entity_ids: Comma-separated list of entity IRIs.
"""
try:
client = get_client()
id_list = [id.strip() for id in entity_ids.split(",")]
for entity_id in id_list:
client.delete_entity(entity_id)
return f"Successfully deleted {len(id_list)} entities from WordLift KG."
except Exception as e:
logger.error(f"Delete error: {e}")
return f"Error: {str(e)}"
if __name__ == "__main__":
mcp.run()