clarifai 11.4.1__py3-none-any.whl → 11.4.3rc1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (142) hide show
  1. clarifai/__init__.py +1 -1
  2. clarifai/__pycache__/__init__.cpython-312.pyc +0 -0
  3. clarifai/__pycache__/__init__.cpython-39.pyc +0 -0
  4. clarifai/__pycache__/errors.cpython-312.pyc +0 -0
  5. clarifai/__pycache__/errors.cpython-39.pyc +0 -0
  6. clarifai/__pycache__/versions.cpython-312.pyc +0 -0
  7. clarifai/__pycache__/versions.cpython-39.pyc +0 -0
  8. clarifai/cli/__pycache__/__init__.cpython-312.pyc +0 -0
  9. clarifai/cli/__pycache__/base.cpython-312.pyc +0 -0
  10. clarifai/cli/__pycache__/compute_cluster.cpython-312.pyc +0 -0
  11. clarifai/cli/__pycache__/deployment.cpython-312.pyc +0 -0
  12. clarifai/cli/__pycache__/model.cpython-312.pyc +0 -0
  13. clarifai/cli/__pycache__/nodepool.cpython-312.pyc +0 -0
  14. clarifai/cli/base.py +8 -0
  15. clarifai/cli/model.py +6 -6
  16. clarifai/client/__pycache__/__init__.cpython-312.pyc +0 -0
  17. clarifai/client/__pycache__/__init__.cpython-39.pyc +0 -0
  18. clarifai/client/__pycache__/app.cpython-312.pyc +0 -0
  19. clarifai/client/__pycache__/app.cpython-39.pyc +0 -0
  20. clarifai/client/__pycache__/base.cpython-312.pyc +0 -0
  21. clarifai/client/__pycache__/base.cpython-39.pyc +0 -0
  22. clarifai/client/__pycache__/compute_cluster.cpython-312.pyc +0 -0
  23. clarifai/client/__pycache__/dataset.cpython-312.pyc +0 -0
  24. clarifai/client/__pycache__/deployment.cpython-312.pyc +0 -0
  25. clarifai/client/__pycache__/input.cpython-312.pyc +0 -0
  26. clarifai/client/__pycache__/lister.cpython-312.pyc +0 -0
  27. clarifai/client/__pycache__/model.cpython-312.pyc +0 -0
  28. clarifai/client/__pycache__/model_client.cpython-312.pyc +0 -0
  29. clarifai/client/__pycache__/module.cpython-312.pyc +0 -0
  30. clarifai/client/__pycache__/nodepool.cpython-312.pyc +0 -0
  31. clarifai/client/__pycache__/runner.cpython-312.pyc +0 -0
  32. clarifai/client/__pycache__/search.cpython-312.pyc +0 -0
  33. clarifai/client/__pycache__/user.cpython-312.pyc +0 -0
  34. clarifai/client/__pycache__/workflow.cpython-312.pyc +0 -0
  35. clarifai/client/auth/__pycache__/__init__.cpython-312.pyc +0 -0
  36. clarifai/client/auth/__pycache__/__init__.cpython-39.pyc +0 -0
  37. clarifai/client/auth/__pycache__/helper.cpython-312.pyc +0 -0
  38. clarifai/client/auth/__pycache__/helper.cpython-39.pyc +0 -0
  39. clarifai/client/auth/__pycache__/register.cpython-312.pyc +0 -0
  40. clarifai/client/auth/__pycache__/register.cpython-39.pyc +0 -0
  41. clarifai/client/auth/__pycache__/stub.cpython-312.pyc +0 -0
  42. clarifai/client/auth/__pycache__/stub.cpython-39.pyc +0 -0
  43. clarifai/client/dataset.py +6 -0
  44. clarifai/constants/__pycache__/base.cpython-312.pyc +0 -0
  45. clarifai/constants/__pycache__/base.cpython-39.pyc +0 -0
  46. clarifai/constants/__pycache__/dataset.cpython-312.pyc +0 -0
  47. clarifai/constants/__pycache__/input.cpython-312.pyc +0 -0
  48. clarifai/constants/__pycache__/model.cpython-312.pyc +0 -0
  49. clarifai/constants/__pycache__/rag.cpython-312.pyc +0 -0
  50. clarifai/constants/__pycache__/search.cpython-312.pyc +0 -0
  51. clarifai/constants/__pycache__/workflow.cpython-312.pyc +0 -0
  52. clarifai/datasets/__pycache__/__init__.cpython-312.pyc +0 -0
  53. clarifai/datasets/export/__pycache__/__init__.cpython-312.pyc +0 -0
  54. clarifai/datasets/export/__pycache__/inputs_annotations.cpython-312.pyc +0 -0
  55. clarifai/datasets/upload/__pycache__/__init__.cpython-312.pyc +0 -0
  56. clarifai/datasets/upload/__pycache__/base.cpython-312.pyc +0 -0
  57. clarifai/datasets/upload/__pycache__/features.cpython-312.pyc +0 -0
  58. clarifai/datasets/upload/__pycache__/image.cpython-312.pyc +0 -0
  59. clarifai/datasets/upload/__pycache__/multimodal.cpython-312.pyc +0 -0
  60. clarifai/datasets/upload/__pycache__/text.cpython-312.pyc +0 -0
  61. clarifai/datasets/upload/__pycache__/utils.cpython-312.pyc +0 -0
  62. clarifai/datasets/upload/loaders/__pycache__/__init__.cpython-312.pyc +0 -0
  63. clarifai/datasets/upload/loaders/__pycache__/coco_detection.cpython-312.pyc +0 -0
  64. clarifai/datasets/upload/loaders/__pycache__/imagenet_classification.cpython-312.pyc +0 -0
  65. clarifai/modules/__pycache__/__init__.cpython-312.pyc +0 -0
  66. clarifai/modules/__pycache__/css.cpython-312.pyc +0 -0
  67. clarifai/rag/__pycache__/__init__.cpython-312.pyc +0 -0
  68. clarifai/rag/__pycache__/rag.cpython-312.pyc +0 -0
  69. clarifai/rag/__pycache__/utils.cpython-312.pyc +0 -0
  70. clarifai/runners/__pycache__/__init__.cpython-312.pyc +0 -0
  71. clarifai/runners/__pycache__/__init__.cpython-39.pyc +0 -0
  72. clarifai/runners/__pycache__/server.cpython-312.pyc +0 -0
  73. clarifai/runners/models/__pycache__/__init__.cpython-312.pyc +0 -0
  74. clarifai/runners/models/__pycache__/__init__.cpython-39.pyc +0 -0
  75. clarifai/runners/models/__pycache__/base_typed_model.cpython-312.pyc +0 -0
  76. clarifai/runners/models/__pycache__/mcp_class.cpython-312.pyc +0 -0
  77. clarifai/runners/models/__pycache__/model_builder.cpython-312.pyc +0 -0
  78. clarifai/runners/models/__pycache__/model_builder.cpython-39.pyc +0 -0
  79. clarifai/runners/models/__pycache__/model_class.cpython-312.pyc +0 -0
  80. clarifai/runners/models/__pycache__/model_run_locally.cpython-312.pyc +0 -0
  81. clarifai/runners/models/__pycache__/model_runner.cpython-312.pyc +0 -0
  82. clarifai/runners/models/__pycache__/model_servicer.cpython-312.pyc +0 -0
  83. clarifai/runners/models/__pycache__/test_model_builder.cpython-312-pytest-8.3.5.pyc +0 -0
  84. clarifai/runners/models/base_typed_model.py +238 -0
  85. clarifai/runners/models/example_mcp_server.py +44 -0
  86. clarifai/runners/models/mcp_class.py +143 -0
  87. clarifai/runners/models/mcp_class.py~ +149 -0
  88. clarifai/runners/models/model_builder.py +167 -38
  89. clarifai/runners/models/model_class.py +5 -22
  90. clarifai/runners/models/model_run_locally.py +0 -4
  91. clarifai/runners/models/test_model_builder.py +89 -0
  92. clarifai/runners/models/visual_classifier_class.py +75 -0
  93. clarifai/runners/models/visual_detector_class.py +79 -0
  94. clarifai/runners/utils/__pycache__/__init__.cpython-312.pyc +0 -0
  95. clarifai/runners/utils/__pycache__/code_script.cpython-312.pyc +0 -0
  96. clarifai/runners/utils/__pycache__/const.cpython-312.pyc +0 -0
  97. clarifai/runners/utils/__pycache__/data_handler.cpython-312.pyc +0 -0
  98. clarifai/runners/utils/__pycache__/data_types.cpython-312.pyc +0 -0
  99. clarifai/runners/utils/__pycache__/data_utils.cpython-312.pyc +0 -0
  100. clarifai/runners/utils/__pycache__/loader.cpython-312.pyc +0 -0
  101. clarifai/runners/utils/__pycache__/method_signatures.cpython-312.pyc +0 -0
  102. clarifai/runners/utils/__pycache__/serializers.cpython-312.pyc +0 -0
  103. clarifai/runners/utils/__pycache__/url_fetcher.cpython-312.pyc +0 -0
  104. clarifai/runners/utils/code_script.py +41 -44
  105. clarifai/runners/utils/const.py +15 -0
  106. clarifai/runners/utils/data_handler.py +231 -0
  107. clarifai/runners/utils/data_types/__pycache__/__init__.cpython-312.pyc +0 -0
  108. clarifai/runners/utils/data_types/__pycache__/data_types.cpython-312.pyc +0 -0
  109. clarifai/runners/utils/data_utils.py +33 -5
  110. clarifai/runners/utils/loader.py +23 -2
  111. clarifai/runners/utils/method_signatures.py +4 -4
  112. clarifai/schema/__pycache__/search.cpython-312.pyc +0 -0
  113. clarifai/urls/__pycache__/helper.cpython-312.pyc +0 -0
  114. clarifai/utils/__pycache__/__init__.cpython-312.pyc +0 -0
  115. clarifai/utils/__pycache__/__init__.cpython-39.pyc +0 -0
  116. clarifai/utils/__pycache__/cli.cpython-312.pyc +0 -0
  117. clarifai/utils/__pycache__/config.cpython-312.pyc +0 -0
  118. clarifai/utils/__pycache__/constants.cpython-312.pyc +0 -0
  119. clarifai/utils/__pycache__/constants.cpython-39.pyc +0 -0
  120. clarifai/utils/__pycache__/logging.cpython-312.pyc +0 -0
  121. clarifai/utils/__pycache__/logging.cpython-39.pyc +0 -0
  122. clarifai/utils/__pycache__/misc.cpython-312.pyc +0 -0
  123. clarifai/utils/__pycache__/misc.cpython-39.pyc +0 -0
  124. clarifai/utils/__pycache__/model_train.cpython-312.pyc +0 -0
  125. clarifai/utils/__pycache__/protobuf.cpython-312.pyc +0 -0
  126. clarifai/utils/config.py +19 -0
  127. clarifai/utils/config.py~ +145 -0
  128. clarifai/utils/evaluation/__pycache__/__init__.cpython-312.pyc +0 -0
  129. clarifai/utils/evaluation/__pycache__/helpers.cpython-312.pyc +0 -0
  130. clarifai/utils/evaluation/__pycache__/main.cpython-312.pyc +0 -0
  131. clarifai/utils/logging.py +22 -5
  132. clarifai/workflows/__pycache__/__init__.cpython-312.pyc +0 -0
  133. clarifai/workflows/__pycache__/export.cpython-312.pyc +0 -0
  134. clarifai/workflows/__pycache__/utils.cpython-312.pyc +0 -0
  135. clarifai/workflows/__pycache__/validate.cpython-312.pyc +0 -0
  136. {clarifai-11.4.1.dist-info → clarifai-11.4.3rc1.dist-info}/METADATA +2 -14
  137. clarifai-11.4.3rc1.dist-info/RECORD +230 -0
  138. {clarifai-11.4.1.dist-info → clarifai-11.4.3rc1.dist-info}/WHEEL +1 -1
  139. clarifai-11.4.1.dist-info/RECORD +0 -109
  140. {clarifai-11.4.1.dist-info/licenses → clarifai-11.4.3rc1.dist-info}/LICENSE +0 -0
  141. {clarifai-11.4.1.dist-info → clarifai-11.4.3rc1.dist-info}/entry_points.txt +0 -0
  142. {clarifai-11.4.1.dist-info → clarifai-11.4.3rc1.dist-info}/top_level.txt +0 -0
clarifai/__init__.py CHANGED
@@ -1 +1 @@
1
- __version__ = "11.4.1"
1
+ __version__ = "11.4.3rc1"
clarifai/cli/base.py CHANGED
@@ -143,6 +143,14 @@ def dump(ctx_obj, output_format):
143
143
  json.dump(ctx_obj.to_dict(), sys.stdout, indent=2)
144
144
 
145
145
 
146
+ @config.command(['cat'])
147
+ @click.option('-o', '--output-format', default='yaml', type=click.Choice(['yaml', 'json']))
148
+ @click.pass_obj
149
+ def env(ctx_obj, output_format):
150
+ """Print env vars. Use: eval "$(clarifai config env)" """
151
+ ctx_obj.current.print_env_vars()
152
+
153
+
146
154
  @cli.command()
147
155
  @click.argument('api_url', default="https://api.clarifai.com")
148
156
  @click.option('--user_id', required=False, help='User ID')
clarifai/cli/model.py CHANGED
@@ -504,6 +504,10 @@ def local_dev(ctx, model_path):
504
504
 
505
505
  logger.info(f"Current deployment_id: {deployment_id}")
506
506
 
507
+ logger.info(
508
+ f"Full url for the model: /users/{user_id}/apps/{app_id}/models/{model.id}/versions/{version.id}"
509
+ )
510
+
507
511
  # Now that we have all the context in ctx.obj, we need to update the config.yaml in
508
512
  # the model_path directory with the model object containing user_id, app_id, model_id, version_id
509
513
  config_file = os.path.join(model_path, 'config.yaml')
@@ -526,13 +530,9 @@ def local_dev(ctx, model_path):
526
530
  ModelBuilder._backup_config(config_file)
527
531
  ModelBuilder._save_config(config_file, config)
528
532
 
529
- # client_model = Model(
530
- # TODO: once we can generate_client_script from ModelBuilder or similar
531
- # we should be able to put the exact function call in place.
532
- # model_script = model.generate_client_script()
533
-
534
533
  builder = ModelBuilder(model_path, download_validation_only=True)
535
- method_signatures = builder.get_method_signatures()
534
+ # don't mock for local dev since you need the dependencies to run the code anyways.
535
+ method_signatures = builder.get_method_signatures(mocking=False)
536
536
 
537
537
  from clarifai.runners.utils import code_script
538
538
 
@@ -685,6 +685,12 @@ class Dataset(Lister, BaseClient):
685
685
  Note:
686
686
  This is a beta feature and is subject to change.
687
687
  """
688
+ try:
689
+ import rich # noqa: F401
690
+ except ImportError:
691
+ raise UserError(
692
+ "Rich library is not installed. Please install it using pip install rich>=13.4.2"
693
+ )
688
694
  self.logger.info("Getting dataset upload status...")
689
695
  dataset_version_id = uuid.uuid4().hex
690
696
  _ = self.create_version(id=dataset_version_id, description="SDK Upload Status")
@@ -0,0 +1,238 @@
1
+ import itertools
2
+ from typing import Any, Dict, Iterator, List, Tuple
3
+
4
+ import numpy as np
5
+ from clarifai_grpc.grpc.api import resources_pb2, service_pb2
6
+ from clarifai_grpc.grpc.api.service_pb2 import PostModelOutputsRequest
7
+ from google.protobuf import json_format
8
+
9
+ from ..utils.data_handler import InputDataHandler, OutputDataHandler
10
+ from .model_class import ModelClass
11
+
12
+
13
+ class AnyAnyModel(ModelClass):
14
+
15
+ def load_model(self):
16
+ """
17
+ Load inference time artifacts that are called frequently .e.g. models, tokenizers, etc.
18
+ in this method so they are loaded only once for faster inference.
19
+ """
20
+ raise NotImplementedError
21
+
22
+ def parse_input_request(
23
+ self, input_request: service_pb2.PostModelOutputsRequest) -> Tuple[List[Dict], Dict]:
24
+ list_input_dict = [
25
+ InputDataHandler.from_proto(input).to_python() for input in input_request.inputs
26
+ ]
27
+ inference_params = json_format.MessageToDict(
28
+ input_request.model.model_version.output_info.params)
29
+
30
+ return list_input_dict, inference_params
31
+
32
+ def convert_output_to_proto(self, outputs: list):
33
+ assert (isinstance(outputs, Iterator) or isinstance(outputs, list) or
34
+ isinstance(outputs, tuple)), "outputs must be an Iterator"
35
+ output_protos = []
36
+ for output in outputs:
37
+ if isinstance(output, OutputDataHandler):
38
+ output = output.proto
39
+ elif isinstance(output, resources_pb2.Output):
40
+ pass
41
+ else:
42
+ raise NotImplementedError
43
+ output_protos.append(output)
44
+
45
+ return service_pb2.MultiOutputResponse(outputs=output_protos)
46
+
47
+ def predict_wrapper(
48
+ self, request: service_pb2.PostModelOutputsRequest) -> service_pb2.MultiOutputResponse:
49
+ list_dict_input, inference_params = self.parse_input_request(request)
50
+ outputs = self.predict(list_dict_input, inference_parameters=inference_params)
51
+ return self.convert_output_to_proto(outputs)
52
+
53
+ def generate_wrapper(
54
+ self, request: PostModelOutputsRequest) -> Iterator[service_pb2.MultiOutputResponse]:
55
+ list_dict_input, inference_params = self.parse_input_request(request)
56
+ outputs = self.generate(list_dict_input, inference_parameters=inference_params)
57
+ for output in outputs:
58
+ yield self.convert_output_to_proto(output)
59
+
60
+ def _preprocess_stream(
61
+ self, request: Iterator[PostModelOutputsRequest]) -> Iterator[Tuple[List[Dict], List[Dict]]]:
62
+ """Return generator of processed data (from proto to python) and inference parameters like predict and generate"""
63
+ for i, req in enumerate(request):
64
+ input_data, _ = self.parse_input_request(req)
65
+ yield input_data
66
+
67
+ def stream_wrapper(self, request: Iterator[PostModelOutputsRequest]
68
+ ) -> Iterator[service_pb2.MultiOutputResponse]:
69
+ first_request = next(request)
70
+ _, inference_params = self.parse_input_request(first_request)
71
+ request_iterator = itertools.chain([first_request], request)
72
+ outputs = self.stream(self._preprocess_stream(request_iterator), inference_params)
73
+ for output in outputs:
74
+ yield self.convert_output_to_proto(output)
75
+
76
+ def predict(self, input_data: List[Dict],
77
+ inference_parameters: Dict[str, Any] = {}) -> List[OutputDataHandler]:
78
+ """
79
+ Prediction method.
80
+
81
+ Args:
82
+ -----
83
+ - input_data: is list of dict where key is input type name.
84
+ * image: np.ndarray
85
+ * text: str
86
+ * audio: bytes
87
+
88
+ - inference_parameters (Dict[str, Union[bool, str, float, int]]): your inference parameters.
89
+
90
+ Returns:
91
+ --------
92
+ List of OutputDataHandler
93
+ """
94
+ raise NotImplementedError
95
+
96
+ def generate(self, input_data: List[Dict],
97
+ inference_parameters: Dict[str, Any] = {}) -> Iterator[List[OutputDataHandler]]:
98
+ """
99
+ Generate method.
100
+
101
+ Args:
102
+ -----
103
+ - input_data: is list of dict where key is input type name.
104
+ * image: np.ndarray
105
+ * text: str
106
+ * audio: bytes
107
+
108
+ - inference_parameters (Dict[str, Union[bool, str, float, int]]): your inference parameters.
109
+
110
+ Yield:
111
+ --------
112
+ List of OutputDataHandler
113
+ """
114
+ raise NotImplementedError
115
+
116
+ def stream(self, inputs: Iterator[List[Dict[str, Any]]],
117
+ inference_params: Dict[str, Any]) -> Iterator[List[OutputDataHandler]]:
118
+ """
119
+ Stream method.
120
+
121
+ Args:
122
+ -----
123
+ input_request: is an Iterator of Tuple which
124
+ - First element (List[Dict[str, Union[np.ndarray, str, bytes]]]) is list of dict input data type which keys and values are:
125
+ * image: np.ndarray
126
+ * text: str
127
+ * audio: bytes
128
+
129
+ - Second element (Dict[str, Union[bool, str, float, int]]): is a dict of inference_parameters
130
+
131
+ Yield:
132
+ --------
133
+ List of OutputDataHandler
134
+ """
135
+ raise NotImplementedError
136
+
137
+
138
+ class VisualInputModel(AnyAnyModel):
139
+
140
+ def parse_input_request(
141
+ self, input_request: service_pb2.PostModelOutputsRequest) -> Tuple[List[Dict], Dict]:
142
+ list_input_dict = [
143
+ InputDataHandler.from_proto(input).image(format="np") for input in input_request.inputs
144
+ ]
145
+ inference_params = json_format.MessageToDict(
146
+ input_request.model.model_version.output_info.params)
147
+
148
+ return list_input_dict, inference_params
149
+
150
+ def load_model(self):
151
+ """
152
+ Load inference time artifacts that are called frequently .e.g. models, tokenizers, etc.
153
+ in this method so they are loaded only once for faster inference.
154
+ """
155
+ raise NotImplementedError
156
+
157
+ def predict(self, input_data: List[np.ndarray],
158
+ inference_parameters: Dict[str, Any] = {}) -> List[OutputDataHandler]:
159
+ """
160
+ Prediction method.
161
+
162
+ Args:
163
+ -----
164
+ - input_data(List[np.ndarray]): is list of image as np.ndarray type
165
+ - inference_parameters (Dict[str, Union[bool, str, float, int]]): your inference parameters.
166
+
167
+ Returns:
168
+ --------
169
+ List of OutputDataHandler
170
+ """
171
+ raise NotImplementedError
172
+
173
+
174
+ class TextInputModel(AnyAnyModel):
175
+
176
+ def load_model(self):
177
+ """
178
+ Load inference time artifacts that are called frequently .e.g. models, tokenizers, etc.
179
+ in this method so they are loaded only once for faster inference.
180
+ """
181
+ raise NotImplementedError
182
+
183
+ def parse_input_request(
184
+ self, input_request: service_pb2.PostModelOutputsRequest) -> Tuple[List[Dict], Dict]:
185
+ list_input_text = [InputDataHandler.from_proto(input).text for input in input_request.inputs]
186
+ inference_params = json_format.MessageToDict(
187
+ input_request.model.model_version.output_info.params)
188
+
189
+ return list_input_text, inference_params
190
+
191
+ def predict(self, input_data: List[str],
192
+ inference_parameters: Dict[str, Any] = {}) -> List[OutputDataHandler]:
193
+ """
194
+ Prediction method.
195
+
196
+ Args:
197
+ -----
198
+ - input_data(List[str]): is list of text as str type
199
+ - inference_parameters (Dict[str, Union[bool, str, float, int]]): your inference parameters.
200
+
201
+ Returns:
202
+ --------
203
+ List of OutputDataHandler
204
+ """
205
+ raise NotImplementedError
206
+
207
+ def generate(self, input_data: List[str],
208
+ inference_parameters: Dict[str, Any] = {}) -> Iterator[List[OutputDataHandler]]:
209
+ """
210
+ Prediction method.
211
+
212
+ Args:
213
+ -----
214
+ - input_data(List[str]): is list of text as str type
215
+ - inference_parameters (Dict[str, Union[bool, str, float, int]]): your inference parameters.
216
+
217
+ Yield:
218
+ --------
219
+ List of OutputDataHandler
220
+ """
221
+ raise NotImplementedError
222
+
223
+ def stream(self, inputs: Iterator[List[str]],
224
+ inference_params: Dict[str, Any]) -> Iterator[List[OutputDataHandler]]:
225
+ """
226
+ Stream method.
227
+
228
+ Args:
229
+ -----
230
+ input_request: is an Iterator of Tuple which
231
+ - First element (List[str]) is list of input text:
232
+ - Second element (Dict[str, Union[bool, str, float, int]]): is a dict of inference_parameters
233
+
234
+ Yield:
235
+ --------
236
+ List of OutputDataHandler
237
+ """
238
+ raise NotImplementedError
@@ -0,0 +1,44 @@
1
+ """Example of how to create an MCP server using MCPClass."""
2
+
3
+ from typing import Dict
4
+ from fastmcp import Tool
5
+
6
+ from clarifai.runners.models.mcp_class import MCPClass
7
+
8
+
9
+ class ExampleMCPServer(MCPClass):
10
+ """Example MCP server that provides a simple calculator tool."""
11
+
12
+ def __init__(self):
13
+ super().__init__()
14
+
15
+ # Define and register a calculator tool
16
+ calculator_tool = Tool(
17
+ name="calculator",
18
+ description="A simple calculator that can add two numbers",
19
+ parameters={
20
+ "type": "object",
21
+ "properties": {
22
+ "a": {"type": "number", "description": "First number"},
23
+ "b": {"type": "number", "description": "Second number"}
24
+ },
25
+ "required": ["a", "b"]
26
+ }
27
+ )
28
+
29
+ @calculator_tool
30
+ async def add(params: Dict[str, float]) -> float:
31
+ """Add two numbers together."""
32
+ return params["a"] + params["b"]
33
+
34
+ # Register the tool with the MCP server
35
+ self.add_tool(calculator_tool)
36
+
37
+
38
+ # Usage example:
39
+ if __name__ == "__main__":
40
+ # Create and run the server
41
+ server = ExampleMCPServer()
42
+
43
+ # The server is now ready to handle MCP requests through the mcp_transport method
44
+ # For example, a client could send a request to list tools or call the calculator