-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
1011 lines (839 loc) · 32.3 KB
/
server.py
File metadata and controls
1011 lines (839 loc) · 32.3 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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
toio-mcp server - MCP server for toio Core Cube
"""
import asyncio
import logging
import sys
from typing import Dict, List, Optional, Any, Union, Sequence
import typer
from rich.logging import RichHandler
from mcp.server.fastmcp import FastMCP
from toio.cube import ToioCoreCube
from toio.scanner import BLEScanner
from toio.cube.api.id_information import PositionId, StandardId, PositionIdMissed, StandardIdMissed
from toio.cube.api.sound import SoundId, Note, MidiNote
from toio.cube.api.button import ButtonState
from toio.cube.api.indicator import Color, IndicatorParam
from toio.cube.api.sensor import (
PostureDataType,
Posture,
MagneticSensorData,
MotionDetectionData,
PostureAngleEulerData,
PostureAngleQuaternionsData,
PostureAngleHighPrecisionEulerData,
)
from toio.cube.api.configuration import (
MagneticSensorFunction,
MagneticSensorCondition,
)
# Configure logging
logging.basicConfig(
level=logging.CRITICAL,
format="%(message)s",
datefmt="[%X]",
# handlers=[RichHandler(rich_tracebacks=True)],
handlers=[logging.StreamHandler(sys.stderr)]
)
logger = logging.getLogger("toio-mcp")
#################################################
# CubeManager class
#################################################
class CubeManager:
"""
CubeManager class for managing toio Core Cubes
"""
def __init__(self):
"""Initialize CubeManager"""
self._cubes: Dict[str, ToioCoreCube] = {}
self._device_map: Dict[str, str] = {} # device_id -> cube_id
async def scan_cubes(self, num: int = 1, timeout: float = 5.0) -> List[Dict[str, str]]:
"""
Scan for toio Core Cubes
Args:
num: Number of cubes to scan for
timeout: Scan timeout in seconds
Returns:
List of dictionaries containing device information
"""
logger.info(f"Scanning for {num} toio Core Cubes (timeout: {timeout}s)")
try:
devices = await BLEScanner.scan(num=num, timeout=timeout)
result = []
for device in devices:
device_info = {
"device_id": device.device.address,
"name": device.name,
"rssi": device.advertisement.rssi,
}
result.append(device_info)
logger.info(f"Found {len(result)} toio Core Cubes")
return result
except Exception as e:
logger.error(f"Error scanning for toio Core Cubes: {e}")
raise
async def connect_cube(self, device_id: str) -> str:
"""
Connect to a toio Core Cube
Args:
device_id: Device ID to connect to
Returns:
Cube ID of the connected cube
"""
logger.info(f"Connecting to toio Core Cube with device_id: {device_id}")
try:
# Check if already connected
if device_id in self._device_map:
cube_id = self._device_map[device_id]
logger.info(f"Already connected to cube with ID: {cube_id}")
return cube_id
# Scan for the device
devices = await BLEScanner.scan(num=10, timeout=5.0)
target_device = None
for device in devices:
if device.device.address == device_id:
target_device = device
break
if target_device is None:
raise ValueError(f"Device with ID {device_id} not found")
# Connect to the device
cube = ToioCoreCube(target_device.interface)
await cube.connect()
# Generate a cube ID and store the cube
cube_id = f"cube_{len(self._cubes) + 1}"
self._cubes[cube_id] = cube
self._device_map[device_id] = cube_id
logger.info(f"Connected to cube with ID: {cube_id}")
return cube_id
except Exception as e:
logger.error(f"Error connecting to toio Core Cube: {e}")
raise
async def disconnect_cube(self, cube_id: str) -> bool:
"""
Disconnect from a toio Core Cube
Args:
cube_id: Cube ID to disconnect from
Returns:
True if disconnected successfully, False otherwise
"""
logger.info(f"Disconnecting from cube with ID: {cube_id}")
try:
if cube_id not in self._cubes:
logger.warning(f"Cube with ID {cube_id} not found")
return False
cube = self._cubes[cube_id]
await cube.disconnect()
# Remove the cube from the dictionaries
device_id = None
for d_id, c_id in self._device_map.items():
if c_id == cube_id:
device_id = d_id
break
if device_id:
del self._device_map[device_id]
del self._cubes[cube_id]
logger.info(f"Disconnected from cube with ID: {cube_id}")
return True
except Exception as e:
logger.error(f"Error disconnecting from toio Core Cube: {e}")
return False
def get_connected_cubes(self) -> List[str]:
"""
Get a list of connected cube IDs
Returns:
List of connected cube IDs
"""
return list(self._cubes.keys())
def get_cube(self, cube_id: str) -> Optional[ToioCoreCube]:
"""
Get a cube by ID
Args:
cube_id: Cube ID to get
Returns:
ToioCoreCube instance or None if not found
"""
return self._cubes.get(cube_id)
async def disconnect_all(self) -> None:
"""
Disconnect from all connected cubes
"""
logger.info("Disconnecting from all cubes")
cube_ids = list(self._cubes.keys())
for cube_id in cube_ids:
await self.disconnect_cube(cube_id)
#################################################
# Tool functions
#################################################
async def _scan_cubes(cube_manager: CubeManager, num: int = 1, timeout: float = 5.0):
"""
Scan for toio Core Cubes
Args:
cube_manager: CubeManager instance
num: Number of cubes to scan for
timeout: Scan timeout in seconds
Returns:
Dict with list of devices
"""
try:
devices = await cube_manager.scan_cubes(num=num, timeout=timeout)
return {"devices": devices}
except Exception as e:
return {"error": str(e)}
async def _connect_cube(cube_manager: CubeManager, device_id: str):
"""
Connect to a toio Core Cube
Args:
cube_manager: CubeManager instance
device_id: Device ID to connect to
Returns:
Dict with cube ID
"""
try:
cube_id = await cube_manager.connect_cube(device_id)
return {"cube_id": cube_id}
except Exception as e:
return {"error": str(e)}
async def _disconnect_cube(cube_manager: CubeManager, cube_id: str):
"""
Disconnect from a toio Core Cube
Args:
cube_manager: CubeManager instance
cube_id: Cube ID to disconnect from
Returns:
Dict with success status
"""
try:
success = await cube_manager.disconnect_cube(cube_id)
return {"disconnected": success}
except Exception as e:
return {"error": str(e)}
async def _get_connected_cubes(cube_manager: CubeManager):
"""
Get a list of connected cubes
Args:
cube_manager: CubeManager instance
Returns:
Dict with list of cube IDs
"""
try:
cubes = cube_manager.get_connected_cubes()
return {"cubes": cubes}
except Exception as e:
return {"error": str(e)}
async def _motor_control(cube_manager: CubeManager, cube_id: str, left: int, right: int, duration_ms: int = 0):
"""
Control the motors of a toio Core Cube
Args:
cube_manager: CubeManager instance
cube_id: Cube ID to control
left: Left motor speed (-100 to 100)
right: Right motor speed (-100 to 100)
duration_ms: Duration in milliseconds (0 for continuous)
Returns:
Dict with success status
"""
try:
cube = cube_manager.get_cube(cube_id)
if cube is None:
return {"error": f"Cube with ID {cube_id} not found"}
await cube.api.motor.motor_control(left, right, duration_ms)
return {"controlled": True}
except Exception as e:
return {"error": str(e)}
async def _motor_stop(cube_manager: CubeManager, cube_id: str):
"""
Stop the motors of a toio Core Cube
Args:
cube_manager: CubeManager instance
cube_id: Cube ID to stop
Returns:
Dict with success status
"""
try:
cube = cube_manager.get_cube(cube_id)
if cube is None:
return {"error": f"Cube with ID {cube_id} not found"}
await cube.api.motor.motor_control(0, 0)
return {"stopped": True}
except Exception as e:
return {"error": str(e)}
async def _set_indicator(cube_manager: CubeManager, cube_id: str, r: int, g: int, b: int, duration_ms: int = 0):
"""
Set the LED color of a toio Core Cube
Args:
cube_manager: CubeManager instance
cube_id: Cube ID to control
r: Red component (0-255)
g: Green component (0-255)
b: Blue component (0-255)
duration_ms: Duration in milliseconds (0 for continuous)
Returns:
Dict with success status
"""
try:
cube = cube_manager.get_cube(cube_id)
if cube is None:
return {"error": f"Cube with ID {cube_id} not found"}
from toio.cube.api.indicator import Color, IndicatorParam
color = Color(r, g, b)
param = IndicatorParam(duration_ms=duration_ms, color=color)
await cube.api.indicator.turn_on(param)
return {"set": True}
except Exception as e:
return {"error": str(e)}
async def _get_position(cube_manager: CubeManager, cube_id: str):
"""
Get the position of a toio Core Cube
Args:
cube_manager: CubeManager instance
cube_id: Cube ID to get position from
Returns:
Dict with position information
"""
try:
cube = cube_manager.get_cube(cube_id)
if cube is None:
return {"error": f"Cube with ID {cube_id} not found"}
position = await cube.api.id_information.read()
if position is None:
return {"error": "Failed to get position information"}
result = {}
# Handle different types of position information
if isinstance(position, PositionId):
result = {
"type": "position_id",
"center_x": position.center.point.x,
"center_y": position.center.point.y,
"center_angle": position.center.angle,
"sensor_x": position.sensor.point.x,
"sensor_y": position.sensor.point.y,
"sensor_angle": position.sensor.angle,
}
elif isinstance(position, StandardId):
result = {
"type": "standard_id",
"value": position.value,
"angle": position.angle,
}
elif isinstance(position, PositionIdMissed):
result = {
"type": "position_id_missed",
}
elif isinstance(position, StandardIdMissed):
result = {
"type": "standard_id_missed",
}
return result
except Exception as e:
return {"error": str(e)}
async def _play_sound_effect(cube_manager: CubeManager, cube_id: str, sound_id: int, volume: int = 255):
"""
Play a sound effect on a toio Core Cube
Args:
cube_manager: CubeManager instance
cube_id: Cube ID to play sound on
sound_id: Sound effect ID (0-10)
volume: Volume (0-255)
Returns:
Dict with success status
"""
try:
cube = cube_manager.get_cube(cube_id)
if cube is None:
return {"error": f"Cube with ID {cube_id} not found"}
await cube.api.sound.play_sound_effect(sound_id, volume)
return {"played": True}
except Exception as e:
return {"error": str(e)}
async def _play_midi(cube_manager: CubeManager, cube_id: str, note: int, duration_ms: int = 1000, volume: int = 255, repeat: int = 0):
"""
Play a MIDI note on a toio Core Cube
Args:
cube_manager: CubeManager instance
cube_id: Cube ID to play sound on
note: MIDI note number (0-127)
duration_ms: Duration in milliseconds (10-2550)
volume: Volume (0-255)
repeat: Number of repetitions (0: Infinite)
Returns:
Dict with success status
"""
try:
cube = cube_manager.get_cube(cube_id)
if cube is None:
return {"error": f"Cube with ID {cube_id} not found"}
# MidiNoteオブジェクトを作成
midi_note = MidiNote(duration_ms=duration_ms, note=note, volume=volume)
# MidiNoteオブジェクトのリストを作成
midi_notes = [midi_note]
# play_midiメソッドを呼び出す
# await cube.api.sound.play_midi(repeat, midi_notes)
_ = asyncio.create_task(cube.api.sound.play_midi(repeat, midi_notes)) # 非同期で実行
return {"played": True}
except Exception as e:
return {"error": str(e)}
async def _stop_sound(cube_manager: CubeManager, cube_id: str):
"""
Stop sound on a toio Core Cube
Args:
cube_manager: CubeManager instance
cube_id: Cube ID to stop sound on
Returns:
Dict with success status
"""
try:
cube = cube_manager.get_cube(cube_id)
if cube is None:
return {"error": f"Cube with ID {cube_id} not found"}
await cube.api.sound.stop()
return {"stopped": True}
except Exception as e:
return {"error": str(e)}
async def _get_button_state(cube_manager: CubeManager, cube_id: str):
"""
Get the button state of a toio Core Cube
Args:
cube_manager: CubeManager instance
cube_id: Cube ID to get button state from
Returns:
Dict with button state information
"""
try:
cube = cube_manager.get_cube(cube_id)
if cube is None:
return {"error": f"Cube with ID {cube_id} not found"}
button_info = await cube.api.button.read()
if button_info is None:
return {"error": "Failed to get button information"}
return {
"state": "pressed" if button_info.state == ButtonState.PRESSED else "released"
}
except Exception as e:
return {"error": str(e)}
async def _get_battery_level(cube_manager: CubeManager, cube_id: str):
"""
Get the battery level of a toio Core Cube
Args:
cube_manager: CubeManager instance
cube_id: Cube ID to get battery level from
Returns:
Dict with battery level information
"""
try:
cube = cube_manager.get_cube(cube_id)
if cube is None:
return {"error": f"Cube with ID {cube_id} not found"}
battery_info = await cube.api.battery.read()
if battery_info is None:
return {"error": "Failed to get battery information"}
return {
"level": battery_info.battery_level
}
except Exception as e:
return {"error": str(e)}
async def _get_motion_detection(cube_manager: CubeManager, cube_id: str):
"""
Get motion detection information from a toio Core Cube
Args:
cube_manager: CubeManager instance
cube_id: Cube ID to get motion detection from
Returns:
Dict with motion detection information
"""
try:
cube = cube_manager.get_cube(cube_id)
if cube is None:
return {"error": f"Cube with ID {cube_id} not found"}
await cube.api.sensor.request_motion_information()
# The Sensor characteristic is shared with posture / magnetic reads,
# so retry until a motion-detection payload appears.
motion_data = None
for _ in range(10):
await asyncio.sleep(0.05)
data = await cube.api.sensor.read()
if isinstance(data, MotionDetectionData):
motion_data = data
break
if motion_data is None:
return {"error": "Failed to get motion detection information"}
result = {
"type": "motion_detection"
}
for attr in ["horizontal", "collision", "double_tap", "posture", "shake"]:
if hasattr(motion_data, attr):
if attr == "posture" and hasattr(motion_data.posture, "name"):
result[attr] = motion_data.posture.name
else:
result[attr] = getattr(motion_data, attr)
return result
except Exception as e:
return {"error": str(e)}
async def _get_posture_angle(cube_manager: CubeManager, cube_id: str, data_type: int = 1):
"""
Get posture angle information from a toio Core Cube
Args:
cube_manager: CubeManager instance
cube_id: Cube ID to get posture angle from
data_type: Posture data type (1: Euler, 2: Quaternions, 3: HighPrecisionEuler)
Returns:
Dict with posture angle information
"""
try:
cube = cube_manager.get_cube(cube_id)
if cube is None:
return {"error": f"Cube with ID {cube_id} not found"}
if data_type == 2:
posture_data_type = PostureDataType.Quaternions
expected_type = PostureAngleQuaternionsData
elif data_type == 3:
posture_data_type = PostureDataType.HighPrecisionEuler
expected_type = PostureAngleHighPrecisionEulerData
else:
posture_data_type = PostureDataType.Euler
expected_type = PostureAngleEulerData
await cube.api.sensor.request_posture_angle_information(posture_data_type)
# Filter by expected payload type — the Sensor characteristic is
# shared with motion / magnetic reads.
posture_data = None
for _ in range(10):
await asyncio.sleep(0.05)
data = await cube.api.sensor.read()
if isinstance(data, expected_type):
posture_data = data
break
if posture_data is None:
return {"error": "Failed to get posture angle information"}
result = {
"type": "posture_angle"
}
attrs = ["w", "x", "y", "z"] if data_type == 2 else ["roll", "pitch", "yaw"]
for attr in attrs:
if hasattr(posture_data, attr):
result[attr] = getattr(posture_data, attr)
return result
except Exception as e:
return {"error": str(e)}
async def _get_magnetic_sensor(cube_manager: CubeManager, cube_id: str):
"""
Get magnetic sensor information from a toio Core Cube
Args:
cube_manager: CubeManager instance
cube_id: Cube ID to get magnetic sensor information from
Returns:
Dict with magnetic sensor information
"""
cube = cube_manager.get_cube(cube_id)
if cube is None:
return {"error": f"Cube with ID {cube_id} not found"}
enabled = False
try:
# Magnetic sensors are disabled by default per toio spec — enable
# MagneticForce mode to receive state, strength and x/y/z values.
await cube.api.configuration.set_magnetic_sensor(
function_type=MagneticSensorFunction.MagneticForce,
interval_ms=20,
condition=MagneticSensorCondition.Always,
)
enabled = True
await asyncio.sleep(0.1)
await cube.api.sensor.request_magnetic_sensor_information()
magnetic_data = None
for _ in range(10):
await asyncio.sleep(0.05)
data = await cube.api.sensor.read()
if isinstance(data, MagneticSensorData):
magnetic_data = data
break
if magnetic_data is None:
return {"error": "Failed to get magnetic sensor information"}
result = {
"type": "magnetic_sensor"
}
for attr in ["state", "strength", "x", "y", "z"]:
if hasattr(magnetic_data, attr):
result[attr] = getattr(magnetic_data, attr)
return result
except Exception as e:
return {"error": str(e)}
finally:
# Treat this tool as one-shot — stop the 20ms magnetic stream so it
# does not drain power or pollute later motion / posture reads.
if enabled:
try:
await cube.api.configuration.set_magnetic_sensor(
function_type=MagneticSensorFunction.Disable,
interval_ms=0,
condition=MagneticSensorCondition.Always,
)
except Exception:
logger.exception("Failed to disable magnetic sensor after read")
async def _set_repeated_indicator(cube_manager: CubeManager, cube_id: str, repeat: int, params: List[Dict[str, Any]]):
"""
Set repeated LED indicator on a toio Core Cube
Args:
cube_manager: CubeManager instance
cube_id: Cube ID to control
repeat: Number of repetitions (0 for infinite)
params: List of indicator parameters, each with duration_ms, r, g, b
Returns:
Dict with success status
"""
try:
cube = cube_manager.get_cube(cube_id)
if cube is None:
return {"error": f"Cube with ID {cube_id} not found"}
param_list = []
for p in params:
color = Color(p["r"], p["g"], p["b"])
param = IndicatorParam(duration_ms=p["duration_ms"], color=color)
param_list.append(param)
# await cube.api.indicator.repeated_turn_on(repeat, param_list)
_ = asyncio.create_task(cube.api.indicator.repeated_turn_on(repeat, param_list)) # 非同期で実行
return {"set": True}
except Exception as e:
return {"error": str(e)}
async def _turn_off_indicator(cube_manager: CubeManager, cube_id: str, indicator_id: int = None):
"""
Turn off LED indicator on a toio Core Cube
Args:
cube_manager: CubeManager instance
cube_id: Cube ID to control
indicator_id: Indicator ID to turn off (None for all)
Returns:
Dict with success status
"""
try:
cube = cube_manager.get_cube(cube_id)
if cube is None:
return {"error": f"Cube with ID {cube_id} not found"}
if indicator_id is None:
await cube.api.indicator.turn_off_all()
else:
await cube.api.indicator.turn_off(indicator_id)
return {"turned_off": True}
except Exception as e:
return {"error": str(e)}
# Wrapper functions for tools
def _scan_cubes_wrapper(cube_manager: CubeManager):
async def wrapper(num: int = 1, timeout: float = 5.0) -> Dict[str, Any]:
return await _scan_cubes(cube_manager, num, timeout)
return wrapper
def _connect_cube_wrapper(cube_manager: CubeManager):
async def wrapper(device_id: str) -> Dict[str, Any]:
return await _connect_cube(cube_manager, device_id)
return wrapper
def _disconnect_cube_wrapper(cube_manager: CubeManager):
async def wrapper(cube_id: str) -> Dict[str, Any]:
return await _disconnect_cube(cube_manager, cube_id)
return wrapper
def _get_connected_cubes_wrapper(cube_manager: CubeManager):
async def wrapper() -> Dict[str, Any]:
return await _get_connected_cubes(cube_manager)
return wrapper
def _motor_control_wrapper(cube_manager: CubeManager):
async def wrapper(cube_id: str, left: int, right: int, duration_ms: int = 0) -> Dict[str, Any]:
return await _motor_control(cube_manager, cube_id, left, right, duration_ms)
return wrapper
def _motor_stop_wrapper(cube_manager: CubeManager):
async def wrapper(cube_id: str) -> Dict[str, Any]:
return await _motor_stop(cube_manager, cube_id)
return wrapper
def _set_indicator_wrapper(cube_manager: CubeManager):
async def wrapper(cube_id: str, r: int, g: int, b: int, duration_ms: int = 0) -> Dict[str, Any]:
return await _set_indicator(cube_manager, cube_id, r, g, b, duration_ms)
return wrapper
def _get_position_wrapper(cube_manager: CubeManager):
async def wrapper(cube_id: str) -> Dict[str, Any]:
return await _get_position(cube_manager, cube_id)
return wrapper
def _play_sound_effect_wrapper(cube_manager: CubeManager):
async def wrapper(cube_id: str, sound_id: int, volume: int = 255) -> Dict[str, Any]:
return await _play_sound_effect(cube_manager, cube_id, sound_id, volume)
return wrapper
def _play_midi_wrapper(cube_manager: CubeManager):
async def wrapper(cube_id: str, note: int, duration_ms: int = 1000, volume: int = 255, repeat: int = 0) -> Dict[str, Any]:
return await _play_midi(cube_manager, cube_id, note, duration_ms, volume, repeat)
return wrapper
def _stop_sound_wrapper(cube_manager: CubeManager):
async def wrapper(cube_id: str) -> Dict[str, Any]:
return await _stop_sound(cube_manager, cube_id)
return wrapper
def _get_button_state_wrapper(cube_manager: CubeManager):
async def wrapper(cube_id: str) -> Dict[str, Any]:
return await _get_button_state(cube_manager, cube_id)
return wrapper
def _get_battery_level_wrapper(cube_manager: CubeManager):
async def wrapper(cube_id: str) -> Dict[str, Any]:
return await _get_battery_level(cube_manager, cube_id)
return wrapper
def _get_motion_detection_wrapper(cube_manager: CubeManager):
async def wrapper(cube_id: str) -> Dict[str, Any]:
return await _get_motion_detection(cube_manager, cube_id)
return wrapper
def _get_posture_angle_wrapper(cube_manager: CubeManager):
async def wrapper(cube_id: str, data_type: int = 1) -> Dict[str, Any]:
return await _get_posture_angle(cube_manager, cube_id, data_type)
return wrapper
def _get_magnetic_sensor_wrapper(cube_manager: CubeManager):
async def wrapper(cube_id: str) -> Dict[str, Any]:
return await _get_magnetic_sensor(cube_manager, cube_id)
return wrapper
def _set_repeated_indicator_wrapper(cube_manager: CubeManager):
async def wrapper(cube_id: str, repeat: int, params: List[Dict[str, Any]]) -> Dict[str, Any]:
return await _set_repeated_indicator(cube_manager, cube_id, repeat, params)
return wrapper
def _turn_off_indicator_wrapper(cube_manager: CubeManager):
async def wrapper(cube_id: str, indicator_id: int = None) -> Dict[str, Any]:
return await _turn_off_indicator(cube_manager, cube_id, indicator_id)
return wrapper
#################################################
# ToioMCPServer class
#################################################
class ToioMCPServer:
"""
ToioMCPServer class for providing toio Core Cube functionality via MCP
"""
def __init__(self):
"""Initialize ToioMCPServer"""
self.cube_manager = CubeManager()
self.server = FastMCP(name="toio-mcp")
# Register tools
self._register_tools()
def _register_tools(self):
"""Register all tools with the MCP server"""
# Scanner tools
self.server.add_tool(
_scan_cubes_wrapper(self.cube_manager),
name="scan_cubes",
description="Scan for toio Core Cubes"
)
self.server.add_tool(
_connect_cube_wrapper(self.cube_manager),
name="connect_cube",
description="Connect to a toio Core Cube"
)
self.server.add_tool(
_disconnect_cube_wrapper(self.cube_manager),
name="disconnect_cube",
description="Disconnect from a toio Core Cube"
)
self.server.add_tool(
_get_connected_cubes_wrapper(self.cube_manager),
name="get_connected_cubes",
description="Get a list of connected cubes"
)
# Motor tools
self.server.add_tool(
_motor_control_wrapper(self.cube_manager),
name="motor_control",
description="Control the motors of a toio Core Cube"
)
self.server.add_tool(
_motor_stop_wrapper(self.cube_manager),
name="motor_stop",
description="Stop the motors of a toio Core Cube"
)
# LED tools
self.server.add_tool(
_set_indicator_wrapper(self.cube_manager),
name="set_indicator",
description="Set the LED color of a toio Core Cube"
)
# Position tools
self.server.add_tool(
_get_position_wrapper(self.cube_manager),
name="get_position",
description="Get the position of a toio Core Cube"
)
# サウンド関連ツール
self.server.add_tool(
_play_sound_effect_wrapper(self.cube_manager),
name="play_sound_effect",
description="Play a sound effect on a toio Core Cube"
)
self.server.add_tool(
_play_midi_wrapper(self.cube_manager),
name="play_midi",
description="Play a MIDI note on a toio Core Cube"
)
self.server.add_tool(
_stop_sound_wrapper(self.cube_manager),
name="stop_sound",
description="Stop sound on a toio Core Cube"
)
# ボタン関連ツール
self.server.add_tool(
_get_button_state_wrapper(self.cube_manager),
name="get_button_state",
description="Get the button state of a toio Core Cube"
)
# バッテリー関連ツール
self.server.add_tool(
_get_battery_level_wrapper(self.cube_manager),
name="get_battery_level",
description="Get the battery level of a toio Core Cube"
)
# センサー関連ツール
self.server.add_tool(
_get_motion_detection_wrapper(self.cube_manager),
name="get_motion_detection",
description="Get motion detection information from a toio Core Cube"
)
self.server.add_tool(
_get_posture_angle_wrapper(self.cube_manager),
name="get_posture_angle",
description="Get posture angle information from a toio Core Cube"
)
self.server.add_tool(
_get_magnetic_sensor_wrapper(self.cube_manager),
name="get_magnetic_sensor",
description="Get magnetic sensor information from a toio Core Cube"
)
# 追加のLEDインジケーター関連ツール
self.server.add_tool(
_set_repeated_indicator_wrapper(self.cube_manager),
name="set_repeated_indicator",
description="Set repeated LED indicator on a toio Core Cube"
)
self.server.add_tool(
_turn_off_indicator_wrapper(self.cube_manager),
name="turn_off_indicator",
description="Turn off LED indicator on a toio Core Cube"
)
def start(self):
"""Start the MCP server"""
self.server.run(transport='stdio')
async def stop(self):
"""Stop the MCP server"""
await self.cube_manager.disconnect_all()
#################################################
# CLI interface
#################################################
app = typer.Typer()
@app.command()
def main(
host: str = typer.Option("127.0.0.1", help="Host to bind to"),
port: int = typer.Option(8000, help="Port to bind to"),
debug: bool = typer.Option(False, help="Enable debug logging"),
):
"""
Start the toio-mcp server
"""
if debug:
logging.getLogger().setLevel(logging.CRITICAL)
logger.debug("Debug logging enabled")
logger.info(f"Starting toio-mcp server on {host}:{port}")
server = ToioMCPServer()
try:
server.start()