| 1 | package MCP::Picnic; | ||
| 2 | # ABSTRACT: MCP Server for Picnic Supermarket API | ||
| 3 | |||
| 4 | 3 | ▶ | use Moo; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 0 | ▶ | use MCP::Server; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 6 | use WWW::Picnic; | ||
| 7 | use JSON::MaybeXS; | ||
| 8 | use Carp qw( croak ); | ||
| 9 | use namespace::clean; | ||
| 10 | |||
| 11 | our $VERSION = '0.001'; | ||
| 12 | |||
| 13 | has user => ( | ||
| 14 | is => 'ro', | ||
| 15 | lazy => 1, | ||
| 16 | default => sub { $ENV{PICNIC_USER} // croak "PICNIC_USER environment variable required" }, | ||
| 17 | ); | ||
| 18 | |||
| 19 | has pass => ( | ||
| 20 | is => 'ro', | ||
| 21 | lazy => 1, | ||
| 22 | default => sub { $ENV{PICNIC_PASS} // croak "PICNIC_PASS environment variable required" }, | ||
| 23 | ); | ||
| 24 | |||
| 25 | has country => ( | ||
| 26 | is => 'ro', | ||
| 27 | lazy => 1, | ||
| 28 | default => sub { $ENV{PICNIC_COUNTRY} // 'de' }, | ||
| 29 | ); | ||
| 30 | |||
| 31 | has picnic => ( | ||
| 32 | is => 'ro', | ||
| 33 | lazy => 1, | ||
| 34 | builder => '_build_picnic', | ||
| 35 | ); | ||
| 36 | |||
| 37 | has json => ( | ||
| 38 | is => 'ro', | ||
| 39 | lazy => 1, | ||
| 40 | default => sub { JSON::MaybeXS->new(utf8 => 1, canonical => 1, convert_blessed => 1) }, | ||
| 41 | ); | ||
| 42 | |||
| 43 | has server => ( | ||
| 44 | is => 'ro', | ||
| 45 | lazy => 1, | ||
| 46 | builder => '_build_server', | ||
| 47 | ); | ||
| 48 | |||
| 49 | has _auth_state => ( | ||
| 50 | is => 'rw', | ||
| 51 | default => sub { 'none' }, # none, pending_2fa, authenticated | ||
| 52 | ); | ||
| 53 | |||
| 54 | sub _build_picnic { | ||
| 55 | my ($self) = @_; | ||
| 56 | return WWW::Picnic->new( | ||
| 57 | user => $self->user, | ||
| 58 | pass => $self->pass, | ||
| 59 | country => $self->country, | ||
| 60 | ); | ||
| 61 | } | ||
| 62 | |||
| 63 | sub _to_json { | ||
| 64 | my ($self, $data) = @_; | ||
| 65 | return $self->json->encode($data); | ||
| 66 | } | ||
| 67 | |||
| 68 | sub _ensure_auth { | ||
| 69 | my ($self) = @_; | ||
| 70 | |||
| 71 | return 1 if $self->_auth_state eq 'authenticated'; | ||
| 72 | |||
| 73 | if ($self->_auth_state eq 'pending_2fa') { | ||
| 74 | return { error => 1, message => "2FA verification pending. Please provide the SMS code you received using the verify_2fa tool." }; | ||
| 75 | } | ||
| 76 | |||
| 77 | # Try to login | ||
| 78 | my $login = eval { $self->picnic->login }; | ||
| 79 | if ($@) { | ||
| 80 | return { error => 1, message => "Login failed: $@" }; | ||
| 81 | } | ||
| 82 | |||
| 83 | if ($login->requires_2fa) { | ||
| 84 | $self->_auth_state('pending_2fa'); | ||
| 85 | eval { $self->picnic->generate_2fa_code }; | ||
| 86 | if ($@) { | ||
| 87 | return { error => 1, message => "Could not request 2FA code: $@" }; | ||
| 88 | } | ||
| 89 | return { | ||
| 90 | error => 1, | ||
| 91 | message => "2FA required. An SMS with a verification code has been sent to your phone. Please provide the code so it can be verified with the verify_2fa tool." | ||
| 92 | }; | ||
| 93 | } | ||
| 94 | |||
| 95 | $self->_auth_state('authenticated'); | ||
| 96 | return 1; | ||
| 97 | } | ||
| 98 | |||
| 99 | sub _article_to_hash { | ||
| 100 | my ($self, $article) = @_; | ||
| 101 | return { | ||
| 102 | id => $article->id, | ||
| 103 | name => $article->name, | ||
| 104 | price => $article->price, | ||
| 105 | display_price => $article->display_price, | ||
| 106 | unit_quantity => $article->unit_quantity, | ||
| 107 | image_id => $article->image_id, | ||
| 108 | }; | ||
| 109 | } | ||
| 110 | |||
| 111 | sub _article_detail_to_hash { | ||
| 112 | my ($self, $article) = @_; | ||
| 113 | return { | ||
| 114 | id => $article->id, | ||
| 115 | name => $article->name, | ||
| 116 | price => $article->price, | ||
| 117 | price_info => $article->price_info, | ||
| 118 | description => $article->description, | ||
| 119 | unit_quantity => $article->unit_quantity, | ||
| 120 | image_ids => $article->image_ids, | ||
| 121 | labels => $article->labels, | ||
| 122 | }; | ||
| 123 | } | ||
| 124 | |||
| 125 | sub _cart_item_to_hash { | ||
| 126 | my ($self, $item) = @_; | ||
| 127 | return { | ||
| 128 | id => $item->{id}, | ||
| 129 | name => $item->{name}, | ||
| 130 | count => $item->{count}, | ||
| 131 | price => $item->{price}, | ||
| 132 | }; | ||
| 133 | } | ||
| 134 | |||
| 135 | sub _cart_to_hash { | ||
| 136 | my ($self, $cart) = @_; | ||
| 137 | return { | ||
| 138 | total_count => $cart->total_count, | ||
| 139 | total_price => $cart->total_price, | ||
| 140 | items => [ map { $self->_cart_item_to_hash($_) } @{ $cart->items } ], | ||
| 141 | delivery_slot => $cart->selected_slot, | ||
| 142 | }; | ||
| 143 | } | ||
| 144 | |||
| 145 | sub _slot_to_hash { | ||
| 146 | my ($self, $slot) = @_; | ||
| 147 | return { | ||
| 148 | slot_id => $slot->slot_id, | ||
| 149 | window_start => $slot->window_start, | ||
| 150 | window_end => $slot->window_end, | ||
| 151 | is_available => $slot->is_available, | ||
| 152 | minimum_order_value => $slot->minimum_order_value, | ||
| 153 | }; | ||
| 154 | } | ||
| 155 | |||
| 156 | sub _user_to_hash { | ||
| 157 | my ($self, $user) = @_; | ||
| 158 | return { | ||
| 159 | user_id => $user->user_id, | ||
| 160 | firstname => $user->firstname, | ||
| 161 | lastname => $user->lastname, | ||
| 162 | address => $user->address, | ||
| 163 | phone => $user->phone, | ||
| 164 | }; | ||
| 165 | } | ||
| 166 | |||
| 167 | sub _suggestion_to_hash { | ||
| 168 | my ($self, $suggestion) = @_; | ||
| 169 | return { | ||
| 170 | suggestion => $suggestion->{suggestion}, | ||
| 171 | type => $suggestion->{type}, | ||
| 172 | }; | ||
| 173 | } | ||
| 174 | |||
| 175 | sub _category_to_hash { | ||
| 176 | my ($self, $category) = @_; | ||
| 177 | return { | ||
| 178 | id => $category->{id}, | ||
| 179 | name => $category->{name}, | ||
| 180 | type => $category->{type}, | ||
| 181 | level => $category->{level}, | ||
| 182 | }; | ||
| 183 | } | ||
| 184 | |||
| 185 | sub _build_server { | ||
| 186 | my ($self) = @_; | ||
| 187 | |||
| 188 | my $server = MCP::Server->new( | ||
| 189 | name => 'Picnic', | ||
| 190 | version => $VERSION, | ||
| 191 | ); | ||
| 192 | |||
| 193 | # Tool: verify_2fa | ||
| 194 | $server->tool( | ||
| 195 | name => 'verify_2fa', | ||
| 196 | description => 'Verify the 2FA code sent via SMS. Use this after the user has provided the code.', | ||
| 197 | input_schema => { | ||
| 198 | type => 'object', | ||
| 199 | properties => { | ||
| 200 | code => { | ||
| 201 | type => 'string', | ||
| 202 | description => 'The 6-digit code from the SMS', | ||
| 203 | }, | ||
| 204 | }, | ||
| 205 | required => ['code'], | ||
| 206 | }, | ||
| 207 | code => sub { | ||
| 208 | my ($tool, $args) = @_; | ||
| 209 | |||
| 210 | unless ($self->_auth_state eq 'pending_2fa') { | ||
| 211 | return $tool->text_result("No 2FA verification pending. Login happens automatically when needed."); | ||
| 212 | } | ||
| 213 | |||
| 214 | my $result = eval { $self->picnic->verify_2fa_code($args->{code}) }; | ||
| 215 | if ($@) { | ||
| 216 | return $tool->text_result("2FA verification failed: $@", 1); | ||
| 217 | } | ||
| 218 | |||
| 219 | $self->_auth_state('authenticated'); | ||
| 220 | return $tool->text_result("Successfully verified! You can now use all Picnic features."); | ||
| 221 | }, | ||
| 222 | ); | ||
| 223 | |||
| 224 | # Tool: search_products | ||
| 225 | $server->tool( | ||
| 226 | name => 'search_products', | ||
| 227 | description => 'Search for products in the Picnic supermarket. Returns products with name, price and ID.', | ||
| 228 | input_schema => { | ||
| 229 | type => 'object', | ||
| 230 | properties => { | ||
| 231 | query => { | ||
| 232 | type => 'string', | ||
| 233 | description => 'Search term (e.g. "milk", "Haribo", "organic eggs")', | ||
| 234 | }, | ||
| 235 | }, | ||
| 236 | required => ['query'], | ||
| 237 | }, | ||
| 238 | code => sub { | ||
| 239 | my ($tool, $args) = @_; | ||
| 240 | |||
| 241 | my $auth = $self->_ensure_auth; | ||
| 242 | return $tool->text_result($auth->{message}, 1) if ref $auth && $auth->{error}; | ||
| 243 | |||
| 244 | my $results = eval { $self->picnic->search($args->{query}) }; | ||
| 245 | return $tool->text_result("Search failed: $@", 1) if $@; | ||
| 246 | |||
| 247 | my @items = map { $self->_article_to_hash($_) } $results->all_items; | ||
| 248 | return $tool->text_result("No products found for '$args->{query}'") unless @items; | ||
| 249 | |||
| 250 | return $tool->text_result($self->_to_json(\@items)); | ||
| 251 | }, | ||
| 252 | ); | ||
| 253 | |||
| 254 | # Tool: get_product_details | ||
| 255 | $server->tool( | ||
| 256 | name => 'get_product_details', | ||
| 257 | description => 'Get detailed information about a product by its ID.', | ||
| 258 | input_schema => { | ||
| 259 | type => 'object', | ||
| 260 | properties => { | ||
| 261 | product_id => { | ||
| 262 | type => 'string', | ||
| 263 | description => 'The product ID (from search)', | ||
| 264 | }, | ||
| 265 | }, | ||
| 266 | required => ['product_id'], | ||
| 267 | }, | ||
| 268 | code => sub { | ||
| 269 | my ($tool, $args) = @_; | ||
| 270 | |||
| 271 | my $auth = $self->_ensure_auth; | ||
| 272 | return $tool->text_result($auth->{message}, 1) if ref $auth && $auth->{error}; | ||
| 273 | |||
| 274 | my $article = eval { $self->picnic->get_article($args->{product_id}) }; | ||
| 275 | return $tool->text_result("Product not found: $@", 1) if $@; | ||
| 276 | |||
| 277 | return $tool->text_result($self->_to_json($self->_article_detail_to_hash($article))); | ||
| 278 | }, | ||
| 279 | ); | ||
| 280 | |||
| 281 | # Tool: get_suggestions | ||
| 282 | $server->tool( | ||
| 283 | name => 'get_suggestions', | ||
| 284 | description => 'Get search suggestions for a partial search term.', | ||
| 285 | input_schema => { | ||
| 286 | type => 'object', | ||
| 287 | properties => { | ||
| 288 | term => { | ||
| 289 | type => 'string', | ||
| 290 | description => 'Partial search term (e.g. "mil" for milk suggestions)', | ||
| 291 | }, | ||
| 292 | }, | ||
| 293 | required => ['term'], | ||
| 294 | }, | ||
| 295 | code => sub { | ||
| 296 | my ($tool, $args) = @_; | ||
| 297 | |||
| 298 | my $auth = $self->_ensure_auth; | ||
| 299 | return $tool->text_result($auth->{message}, 1) if ref $auth && $auth->{error}; | ||
| 300 | |||
| 301 | my $suggestions = eval { $self->picnic->get_suggestions($args->{term}) }; | ||
| 302 | return $tool->text_result("Suggestions failed: $@", 1) if $@; | ||
| 303 | |||
| 304 | my @suggestions = map { $self->_suggestion_to_hash($_) } $suggestions->all_suggestions; | ||
| 305 | return $tool->text_result($self->_to_json(\@suggestions)); | ||
| 306 | }, | ||
| 307 | ); | ||
| 308 | |||
| 309 | # Tool: get_cart | ||
| 310 | $server->tool( | ||
| 311 | name => 'get_cart', | ||
| 312 | description => 'Show the current cart with all items, total price and selected delivery slot.', | ||
| 313 | input_schema => { | ||
| 314 | type => 'object', | ||
| 315 | properties => {}, | ||
| 316 | }, | ||
| 317 | code => sub { | ||
| 318 | my ($tool, $args) = @_; | ||
| 319 | |||
| 320 | my $auth = $self->_ensure_auth; | ||
| 321 | return $tool->text_result($auth->{message}, 1) if ref $auth && $auth->{error}; | ||
| 322 | |||
| 323 | my $cart = eval { $self->picnic->get_cart }; | ||
| 324 | return $tool->text_result("Could not load cart: $@", 1) if $@; | ||
| 325 | |||
| 326 | return $tool->text_result($self->_to_json($self->_cart_to_hash($cart))); | ||
| 327 | }, | ||
| 328 | ); | ||
| 329 | |||
| 330 | # Tool: add_to_cart | ||
| 331 | $server->tool( | ||
| 332 | name => 'add_to_cart', | ||
| 333 | description => 'Add a product to the cart.', | ||
| 334 | input_schema => { | ||
| 335 | type => 'object', | ||
| 336 | properties => { | ||
| 337 | product_id => { | ||
| 338 | type => 'string', | ||
| 339 | description => 'The product ID', | ||
| 340 | }, | ||
| 341 | count => { | ||
| 342 | type => 'integer', | ||
| 343 | description => 'Quantity (default: 1)', | ||
| 344 | default => 1, | ||
| 345 | }, | ||
| 346 | }, | ||
| 347 | required => ['product_id'], | ||
| 348 | }, | ||
| 349 | code => sub { | ||
| 350 | my ($tool, $args) = @_; | ||
| 351 | |||
| 352 | my $auth = $self->_ensure_auth; | ||
| 353 | return $tool->text_result($auth->{message}, 1) if ref $auth && $auth->{error}; | ||
| 354 | |||
| 355 | my $count = $args->{count} // 1; | ||
| 356 | my $cart = eval { $self->picnic->add_to_cart($args->{product_id}, $count) }; | ||
| 357 | return $tool->text_result("Could not add to cart: $@", 1) if $@; | ||
| 358 | |||
| 359 | return $tool->text_result($self->_to_json({ | ||
| 360 | message => "Product added ($count x)", | ||
| 361 | cart => $self->_cart_to_hash($cart), | ||
| 362 | })); | ||
| 363 | }, | ||
| 364 | ); | ||
| 365 | |||
| 366 | # Tool: remove_from_cart | ||
| 367 | $server->tool( | ||
| 368 | name => 'remove_from_cart', | ||
| 369 | description => 'Remove a product from the cart.', | ||
| 370 | input_schema => { | ||
| 371 | type => 'object', | ||
| 372 | properties => { | ||
| 373 | product_id => { | ||
| 374 | type => 'string', | ||
| 375 | description => 'The product ID', | ||
| 376 | }, | ||
| 377 | count => { | ||
| 378 | type => 'integer', | ||
| 379 | description => 'Quantity to remove (default: 1)', | ||
| 380 | default => 1, | ||
| 381 | }, | ||
| 382 | }, | ||
| 383 | required => ['product_id'], | ||
| 384 | }, | ||
| 385 | code => sub { | ||
| 386 | my ($tool, $args) = @_; | ||
| 387 | |||
| 388 | my $auth = $self->_ensure_auth; | ||
| 389 | return $tool->text_result($auth->{message}, 1) if ref $auth && $auth->{error}; | ||
| 390 | |||
| 391 | my $count = $args->{count} // 1; | ||
| 392 | my $cart = eval { $self->picnic->remove_from_cart($args->{product_id}, $count) }; | ||
| 393 | return $tool->text_result("Could not remove from cart: $@", 1) if $@; | ||
| 394 | |||
| 395 | return $tool->text_result($self->_to_json({ | ||
| 396 | message => "Product removed ($count x)", | ||
| 397 | cart => $self->_cart_to_hash($cart), | ||
| 398 | })); | ||
| 399 | }, | ||
| 400 | ); | ||
| 401 | |||
| 402 | # Tool: clear_cart | ||
| 403 | $server->tool( | ||
| 404 | name => 'clear_cart', | ||
| 405 | description => 'Empty the entire cart.', | ||
| 406 | input_schema => { | ||
| 407 | type => 'object', | ||
| 408 | properties => {}, | ||
| 409 | }, | ||
| 410 | code => sub { | ||
| 411 | my ($tool, $args) = @_; | ||
| 412 | |||
| 413 | my $auth = $self->_ensure_auth; | ||
| 414 | return $tool->text_result($auth->{message}, 1) if ref $auth && $auth->{error}; | ||
| 415 | |||
| 416 | my $cart = eval { $self->picnic->clear_cart }; | ||
| 417 | return $tool->text_result("Could not clear cart: $@", 1) if $@; | ||
| 418 | |||
| 419 | return $tool->text_result("Cart cleared!"); | ||
| 420 | }, | ||
| 421 | ); | ||
| 422 | |||
| 423 | # Tool: get_delivery_slots | ||
| 424 | $server->tool( | ||
| 425 | name => 'get_delivery_slots', | ||
| 426 | description => 'Show available delivery time windows.', | ||
| 427 | input_schema => { | ||
| 428 | type => 'object', | ||
| 429 | properties => {}, | ||
| 430 | }, | ||
| 431 | code => sub { | ||
| 432 | my ($tool, $args) = @_; | ||
| 433 | |||
| 434 | my $auth = $self->_ensure_auth; | ||
| 435 | return $tool->text_result($auth->{message}, 1) if ref $auth && $auth->{error}; | ||
| 436 | |||
| 437 | my $slots = eval { $self->picnic->get_delivery_slots }; | ||
| 438 | return $tool->text_result("Could not load delivery slots: $@", 1) if $@; | ||
| 439 | |||
| 440 | my @available = map { $self->_slot_to_hash($_) } $slots->available_slots; | ||
| 441 | return $tool->text_result("No available delivery slots") unless @available; | ||
| 442 | |||
| 443 | return $tool->text_result($self->_to_json(\@available)); | ||
| 444 | }, | ||
| 445 | ); | ||
| 446 | |||
| 447 | # Tool: set_delivery_slot | ||
| 448 | $server->tool( | ||
| 449 | name => 'set_delivery_slot', | ||
| 450 | description => 'Select a delivery slot for the order.', | ||
| 451 | input_schema => { | ||
| 452 | type => 'object', | ||
| 453 | properties => { | ||
| 454 | slot_id => { | ||
| 455 | type => 'string', | ||
| 456 | description => 'The slot ID (from get_delivery_slots)', | ||
| 457 | }, | ||
| 458 | }, | ||
| 459 | required => ['slot_id'], | ||
| 460 | }, | ||
| 461 | code => sub { | ||
| 462 | my ($tool, $args) = @_; | ||
| 463 | |||
| 464 | my $auth = $self->_ensure_auth; | ||
| 465 | return $tool->text_result($auth->{message}, 1) if ref $auth && $auth->{error}; | ||
| 466 | |||
| 467 | my $cart = eval { $self->picnic->set_delivery_slot($args->{slot_id}) }; | ||
| 468 | return $tool->text_result("Could not set delivery slot: $@", 1) if $@; | ||
| 469 | |||
| 470 | return $tool->text_result($self->_to_json({ | ||
| 471 | message => "Delivery slot selected!", | ||
| 472 | cart => $self->_cart_to_hash($cart), | ||
| 473 | })); | ||
| 474 | }, | ||
| 475 | ); | ||
| 476 | |||
| 477 | # Tool: get_user | ||
| 478 | $server->tool( | ||
| 479 | name => 'get_user', | ||
| 480 | description => 'Show information about the logged-in user (name, address, etc.).', | ||
| 481 | input_schema => { | ||
| 482 | type => 'object', | ||
| 483 | properties => {}, | ||
| 484 | }, | ||
| 485 | code => sub { | ||
| 486 | my ($tool, $args) = @_; | ||
| 487 | |||
| 488 | my $auth = $self->_ensure_auth; | ||
| 489 | return $tool->text_result($auth->{message}, 1) if ref $auth && $auth->{error}; | ||
| 490 | |||
| 491 | my $user = eval { $self->picnic->get_user }; | ||
| 492 | return $tool->text_result("Could not load user info: $@", 1) if $@; | ||
| 493 | |||
| 494 | return $tool->text_result($self->_to_json($self->_user_to_hash($user))); | ||
| 495 | }, | ||
| 496 | ); | ||
| 497 | |||
| 498 | # Tool: get_categories | ||
| 499 | $server->tool( | ||
| 500 | name => 'get_categories', | ||
| 501 | description => 'Show the shop\'s product categories.', | ||
| 502 | input_schema => { | ||
| 503 | type => 'object', | ||
| 504 | properties => { | ||
| 505 | depth => { | ||
| 506 | type => 'integer', | ||
| 507 | description => 'Category depth (0 = top-level categories only)', | ||
| 508 | default => 0, | ||
| 509 | }, | ||
| 510 | }, | ||
| 511 | }, | ||
| 512 | code => sub { | ||
| 513 | my ($tool, $args) = @_; | ||
| 514 | |||
| 515 | my $auth = $self->_ensure_auth; | ||
| 516 | return $tool->text_result($auth->{message}, 1) if ref $auth && $auth->{error}; | ||
| 517 | |||
| 518 | my $depth = $args->{depth} // 0; | ||
| 519 | my $categories = eval { $self->picnic->get_categories($depth) }; | ||
| 520 | return $tool->text_result("Could not load categories: $@", 1) if $@; | ||
| 521 | |||
| 522 | my @categories = map { $self->_category_to_hash($_) } $categories->all_categories; | ||
| 523 | return $tool->text_result($self->_to_json(\@categories)); | ||
| 524 | }, | ||
| 525 | ); | ||
| 526 | |||
| 527 | return $server; | ||
| 528 | } | ||
| 529 | |||
| 530 | sub run_stdio { | ||
| 531 | my ($self) = @_; | ||
| 532 | $self = $self->new unless ref $self; | ||
| 533 | $self->server->to_stdio; | ||
| 534 | } | ||
| 535 | |||
| 536 | 1; | ||
| 537 | |||
| 538 | __END__ | ||