-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathcloud_ops.py
More file actions
2162 lines (1942 loc) · 67 KB
/
cloud_ops.py
File metadata and controls
2162 lines (1942 loc) · 67 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
# Copyright (c) 2024 Airbyte, Inc., all rights reserved.
"""Airbyte Cloud MCP operations."""
from pathlib import Path
from typing import Annotated, Any, cast
from fastmcp import FastMCP
from pydantic import BaseModel, Field
from airbyte import cloud, get_destination, get_source
from airbyte._util import api_util
from airbyte.cloud.auth import (
resolve_cloud_api_url,
resolve_cloud_client_id,
resolve_cloud_client_secret,
resolve_cloud_workspace_id,
)
from airbyte.cloud.connectors import CustomCloudSourceDefinition
from airbyte.cloud.constants import FAILED_STATUSES
from airbyte.cloud.workspaces import CloudWorkspace
from airbyte.destinations.util import get_noop_destination
from airbyte.exceptions import AirbyteMissingResourceError, PyAirbyteInputError
from airbyte.mcp._tool_utils import (
check_guid_created_in_session,
mcp_tool,
register_guid_created_in_session,
register_tools,
)
from airbyte.mcp._util import resolve_config, resolve_list_of_strings
from airbyte.secrets import SecretString
CLOUD_AUTH_TIP_TEXT = (
"By default, the `AIRBYTE_CLOUD_CLIENT_ID`, `AIRBYTE_CLOUD_CLIENT_SECRET`, "
"and `AIRBYTE_CLOUD_WORKSPACE_ID` environment variables "
"will be used to authenticate with the Airbyte Cloud API."
)
WORKSPACE_ID_TIP_TEXT = "Workspace ID. Defaults to `AIRBYTE_CLOUD_WORKSPACE_ID` env var."
class CloudSourceResult(BaseModel):
"""Information about a deployed source connector in Airbyte Cloud."""
id: str
"""The source ID."""
name: str
"""Display name of the source."""
url: str
"""Web URL for managing this source in Airbyte Cloud."""
class CloudDestinationResult(BaseModel):
"""Information about a deployed destination connector in Airbyte Cloud."""
id: str
"""The destination ID."""
name: str
"""Display name of the destination."""
url: str
"""Web URL for managing this destination in Airbyte Cloud."""
class CloudConnectionResult(BaseModel):
"""Information about a deployed connection in Airbyte Cloud."""
id: str
"""The connection ID."""
name: str
"""Display name of the connection."""
url: str
"""Web URL for managing this connection in Airbyte Cloud."""
source_id: str
"""ID of the source used by this connection."""
destination_id: str
"""ID of the destination used by this connection."""
last_job_status: str | None = None
"""Status of the most recent completed sync job (e.g., 'succeeded', 'failed', 'cancelled').
Only populated when with_connection_status=True."""
last_job_id: int | None = None
"""Job ID of the most recent completed sync. Only populated when with_connection_status=True."""
last_job_time: str | None = None
"""ISO 8601 timestamp of the most recent completed sync.
Only populated when with_connection_status=True."""
currently_running_job_id: int | None = None
"""Job ID of a currently running sync, if any.
Only populated when with_connection_status=True."""
currently_running_job_start_time: str | None = None
"""ISO 8601 timestamp of when the currently running sync started.
Only populated when with_connection_status=True."""
class CloudSourceDetails(BaseModel):
"""Detailed information about a deployed source connector in Airbyte Cloud."""
source_id: str
"""The source ID."""
source_name: str
"""Display name of the source."""
source_url: str
"""Web URL for managing this source in Airbyte Cloud."""
connector_definition_id: str
"""The connector definition ID (e.g., the ID for 'source-postgres')."""
class CloudDestinationDetails(BaseModel):
"""Detailed information about a deployed destination connector in Airbyte Cloud."""
destination_id: str
"""The destination ID."""
destination_name: str
"""Display name of the destination."""
destination_url: str
"""Web URL for managing this destination in Airbyte Cloud."""
connector_definition_id: str
"""The connector definition ID (e.g., the ID for 'destination-snowflake')."""
class CloudConnectionDetails(BaseModel):
"""Detailed information about a deployed connection in Airbyte Cloud."""
connection_id: str
"""The connection ID."""
connection_name: str
"""Display name of the connection."""
connection_url: str
"""Web URL for managing this connection in Airbyte Cloud."""
source_id: str
"""ID of the source used by this connection."""
source_name: str
"""Display name of the source."""
destination_id: str
"""ID of the destination used by this connection."""
destination_name: str
"""Display name of the destination."""
selected_streams: list[str]
"""List of stream names selected for syncing."""
table_prefix: str | None
"""Table prefix applied when syncing to the destination."""
class CloudOrganizationResult(BaseModel):
"""Information about an organization in Airbyte Cloud."""
id: str
"""The organization ID."""
name: str
"""Display name of the organization."""
email: str
"""Email associated with the organization."""
class CloudWorkspaceResult(BaseModel):
"""Information about a workspace in Airbyte Cloud."""
workspace_id: str
"""The workspace ID."""
workspace_name: str
"""Display name of the workspace."""
workspace_url: str | None = None
"""URL to access the workspace in Airbyte Cloud."""
organization_id: str
"""ID of the organization this workspace belongs to."""
organization_name: str | None = None
"""Name of the organization this workspace belongs to."""
class LogReadResult(BaseModel):
"""Result of reading sync logs with pagination support."""
job_id: int
"""The job ID the logs belong to."""
attempt_number: int
"""The attempt number the logs belong to."""
log_text: str
"""The string containing the log text we are returning."""
log_text_start_line: int
"""1-based line index of the first line returned."""
log_text_line_count: int
"""Count of lines we are returning."""
total_log_lines_available: int
"""Total number of log lines available, shows if any lines were missed due to the limit."""
def _get_cloud_workspace(workspace_id: str | None = None) -> CloudWorkspace:
"""Get an authenticated CloudWorkspace.
Args:
workspace_id: Optional workspace ID. If not provided, uses the
AIRBYTE_CLOUD_WORKSPACE_ID environment variable.
"""
return CloudWorkspace(
workspace_id=resolve_cloud_workspace_id(workspace_id),
client_id=resolve_cloud_client_id(),
client_secret=resolve_cloud_client_secret(),
api_root=resolve_cloud_api_url(),
)
@mcp_tool(
domain="cloud",
open_world=True,
extra_help_text=CLOUD_AUTH_TIP_TEXT,
)
def deploy_source_to_cloud(
source_name: Annotated[
str,
Field(description="The name to use when deploying the source."),
],
source_connector_name: Annotated[
str,
Field(description="The name of the source connector (e.g., 'source-faker')."),
],
*,
workspace_id: Annotated[
str | None,
Field(
description=WORKSPACE_ID_TIP_TEXT,
default=None,
),
],
config: Annotated[
dict | str | None,
Field(
description="The configuration for the source connector.",
default=None,
),
],
config_secret_name: Annotated[
str | None,
Field(
description="The name of the secret containing the configuration.",
default=None,
),
],
unique: Annotated[
bool,
Field(
description="Whether to require a unique name.",
default=True,
),
],
) -> str:
"""Deploy a source connector to Airbyte Cloud."""
source = get_source(
source_connector_name,
no_executor=True,
)
config_dict = resolve_config(
config=config,
config_secret_name=config_secret_name,
config_spec_jsonschema=source.config_spec,
)
source.set_config(config_dict, validate=True)
workspace: CloudWorkspace = _get_cloud_workspace(workspace_id)
deployed_source = workspace.deploy_source(
name=source_name,
source=source,
unique=unique,
)
register_guid_created_in_session(deployed_source.connector_id)
return (
f"Successfully deployed source '{source_name}' with ID '{deployed_source.connector_id}'"
f" and URL: {deployed_source.connector_url}"
)
@mcp_tool(
domain="cloud",
open_world=True,
extra_help_text=CLOUD_AUTH_TIP_TEXT,
)
def deploy_destination_to_cloud(
destination_name: Annotated[
str,
Field(description="The name to use when deploying the destination."),
],
destination_connector_name: Annotated[
str,
Field(description="The name of the destination connector (e.g., 'destination-postgres')."),
],
*,
workspace_id: Annotated[
str | None,
Field(
description=WORKSPACE_ID_TIP_TEXT,
default=None,
),
],
config: Annotated[
dict | str | None,
Field(
description="The configuration for the destination connector.",
default=None,
),
],
config_secret_name: Annotated[
str | None,
Field(
description="The name of the secret containing the configuration.",
default=None,
),
],
unique: Annotated[
bool,
Field(
description="Whether to require a unique name.",
default=True,
),
],
) -> str:
"""Deploy a destination connector to Airbyte Cloud."""
destination = get_destination(
destination_connector_name,
no_executor=True,
)
config_dict = resolve_config(
config=config,
config_secret_name=config_secret_name,
config_spec_jsonschema=destination.config_spec,
)
destination.set_config(config_dict, validate=True)
workspace: CloudWorkspace = _get_cloud_workspace(workspace_id)
deployed_destination = workspace.deploy_destination(
name=destination_name,
destination=destination,
unique=unique,
)
register_guid_created_in_session(deployed_destination.connector_id)
return (
f"Successfully deployed destination '{destination_name}' "
f"with ID: {deployed_destination.connector_id}"
)
@mcp_tool(
domain="cloud",
open_world=True,
extra_help_text=CLOUD_AUTH_TIP_TEXT,
)
def create_connection_on_cloud(
connection_name: Annotated[
str,
Field(description="The name of the connection."),
],
source_id: Annotated[
str,
Field(description="The ID of the deployed source."),
],
destination_id: Annotated[
str,
Field(description="The ID of the deployed destination."),
],
selected_streams: Annotated[
str | list[str],
Field(
description=(
"The selected stream names to sync within the connection. "
"Must be an explicit stream name or list of streams. "
"Cannot be empty or '*'."
)
),
],
*,
workspace_id: Annotated[
str | None,
Field(
description=WORKSPACE_ID_TIP_TEXT,
default=None,
),
],
table_prefix: Annotated[
str | None,
Field(
description="Optional table prefix to use when syncing to the destination.",
default=None,
),
],
) -> str:
"""Create a connection between a deployed source and destination on Airbyte Cloud."""
resolved_streams_list: list[str] = resolve_list_of_strings(selected_streams)
workspace: CloudWorkspace = _get_cloud_workspace(workspace_id)
deployed_connection = workspace.deploy_connection(
connection_name=connection_name,
source=source_id,
destination=destination_id,
selected_streams=resolved_streams_list,
table_prefix=table_prefix,
)
register_guid_created_in_session(deployed_connection.connection_id)
return (
f"Successfully created connection '{connection_name}' "
f"with ID '{deployed_connection.connection_id}' and "
f"URL: {deployed_connection.connection_url}"
)
@mcp_tool(
domain="cloud",
open_world=True,
extra_help_text=CLOUD_AUTH_TIP_TEXT,
)
def run_cloud_sync(
connection_id: Annotated[
str,
Field(description="The ID of the Airbyte Cloud connection."),
],
*,
workspace_id: Annotated[
str | None,
Field(
description=WORKSPACE_ID_TIP_TEXT,
default=None,
),
],
wait: Annotated[
bool,
Field(
description=(
"Whether to wait for the sync to complete. Since a sync can take between several "
"minutes and several hours, this option is not recommended for most "
"scenarios."
),
default=False,
),
],
wait_timeout: Annotated[
int,
Field(
description="Maximum time to wait for sync completion (seconds).",
default=300,
),
],
) -> str:
"""Run a sync job on Airbyte Cloud."""
workspace: CloudWorkspace = _get_cloud_workspace(workspace_id)
connection = workspace.get_connection(connection_id=connection_id)
sync_result = connection.run_sync(wait=wait, wait_timeout=wait_timeout)
if wait:
status = sync_result.get_job_status()
return (
f"Sync completed with status: {status}. "
f"Job ID is '{sync_result.job_id}' and "
f"job URL is: {sync_result.job_url}"
)
return f"Sync started. Job ID is '{sync_result.job_id}' and job URL is: {sync_result.job_url}"
@mcp_tool(
domain="cloud",
read_only=True,
idempotent=True,
open_world=True,
extra_help_text=CLOUD_AUTH_TIP_TEXT,
)
def check_airbyte_cloud_workspace(
*,
workspace_id: Annotated[
str | None,
Field(
description=WORKSPACE_ID_TIP_TEXT,
default=None,
),
],
) -> CloudWorkspaceResult:
"""Check if we have a valid Airbyte Cloud connection and return workspace info.
Returns workspace details including workspace ID, name, and organization info.
"""
workspace: CloudWorkspace = _get_cloud_workspace(workspace_id)
api_root = resolve_cloud_api_url()
client_id = resolve_cloud_client_id()
client_secret = resolve_cloud_client_secret()
# Get workspace details from the public API
workspace_response = api_util.get_workspace(
workspace_id=workspace.workspace_id,
api_root=api_root,
client_id=client_id,
client_secret=client_secret,
)
# Try to get organization info, but fail gracefully if we don't have permissions.
# Fetching organization info requires ORGANIZATION_READER permissions on the organization,
# which may not be available with workspace-scoped credentials.
organization = workspace.get_organization(raise_on_error=False)
return CloudWorkspaceResult(
workspace_id=workspace_response.workspace_id,
workspace_name=workspace_response.name,
workspace_url=workspace.workspace_url,
organization_id=(
organization.organization_id
if organization
else "[unavailable - requires ORGANIZATION_READER permission]"
),
organization_name=organization.organization_name if organization else None,
)
@mcp_tool(
domain="cloud",
open_world=True,
extra_help_text=CLOUD_AUTH_TIP_TEXT,
)
def deploy_noop_destination_to_cloud(
name: str = "No-op Destination",
*,
workspace_id: Annotated[
str | None,
Field(
description=WORKSPACE_ID_TIP_TEXT,
default=None,
),
],
unique: bool = True,
) -> str:
"""Deploy the No-op destination to Airbyte Cloud for testing purposes."""
destination = get_noop_destination()
workspace: CloudWorkspace = _get_cloud_workspace(workspace_id)
deployed_destination = workspace.deploy_destination(
name=name,
destination=destination,
unique=unique,
)
register_guid_created_in_session(deployed_destination.connector_id)
return (
f"Successfully deployed No-op Destination "
f"with ID '{deployed_destination.connector_id}' and "
f"URL: {deployed_destination.connector_url}"
)
@mcp_tool(
domain="cloud",
read_only=True,
idempotent=True,
open_world=True,
extra_help_text=CLOUD_AUTH_TIP_TEXT,
)
def get_cloud_sync_status(
connection_id: Annotated[
str,
Field(
description="The ID of the Airbyte Cloud connection.",
),
],
job_id: Annotated[
int | None,
Field(
description="Optional job ID. If not provided, the latest job will be used.",
default=None,
),
],
*,
workspace_id: Annotated[
str | None,
Field(
description=WORKSPACE_ID_TIP_TEXT,
default=None,
),
],
include_attempts: Annotated[
bool,
Field(
description="Whether to include detailed attempts information.",
default=False,
),
],
) -> dict[str, Any]:
"""Get the status of a sync job from the Airbyte Cloud."""
workspace: CloudWorkspace = _get_cloud_workspace(workspace_id)
connection = workspace.get_connection(connection_id=connection_id)
# If a job ID is provided, get the job by ID.
sync_result: cloud.SyncResult | None = connection.get_sync_result(job_id=job_id)
if not sync_result:
return {"status": None, "job_id": None, "attempts": []}
result = {
"status": sync_result.get_job_status(),
"job_id": sync_result.job_id,
"bytes_synced": sync_result.bytes_synced,
"records_synced": sync_result.records_synced,
"start_time": sync_result.start_time.isoformat(),
"job_url": sync_result.job_url,
"attempts": [],
}
if include_attempts:
attempts = sync_result.get_attempts()
result["attempts"] = [
{
"attempt_number": attempt.attempt_number,
"attempt_id": attempt.attempt_id,
"status": attempt.status,
"bytes_synced": attempt.bytes_synced,
"records_synced": attempt.records_synced,
"created_at": attempt.created_at.isoformat(),
}
for attempt in attempts
]
return result
@mcp_tool(
domain="cloud",
read_only=True,
idempotent=True,
open_world=True,
extra_help_text=CLOUD_AUTH_TIP_TEXT,
)
def list_deployed_cloud_source_connectors(
*,
workspace_id: Annotated[
str | None,
Field(
description=WORKSPACE_ID_TIP_TEXT,
default=None,
),
],
name_contains: Annotated[
str | None,
Field(
description="Optional case-insensitive substring to filter sources by name",
default=None,
),
],
max_items_limit: Annotated[
int | None,
Field(
description="Optional maximum number of items to return (default: no limit)",
default=None,
),
],
) -> list[CloudSourceResult]:
"""List all deployed source connectors in the Airbyte Cloud workspace."""
workspace: CloudWorkspace = _get_cloud_workspace(workspace_id)
sources = workspace.list_sources()
# Filter by name if requested
if name_contains:
needle = name_contains.lower()
sources = [s for s in sources if s.name is not None and needle in s.name.lower()]
# Apply limit if requested
if max_items_limit is not None:
sources = sources[:max_items_limit]
# Note: name and url are guaranteed non-null from list API responses
return [
CloudSourceResult(
id=source.source_id,
name=cast(str, source.name),
url=cast(str, source.connector_url),
)
for source in sources
]
@mcp_tool(
domain="cloud",
read_only=True,
idempotent=True,
open_world=True,
extra_help_text=CLOUD_AUTH_TIP_TEXT,
)
def list_deployed_cloud_destination_connectors(
*,
workspace_id: Annotated[
str | None,
Field(
description=WORKSPACE_ID_TIP_TEXT,
default=None,
),
],
name_contains: Annotated[
str | None,
Field(
description="Optional case-insensitive substring to filter destinations by name",
default=None,
),
],
max_items_limit: Annotated[
int | None,
Field(
description="Optional maximum number of items to return (default: no limit)",
default=None,
),
],
) -> list[CloudDestinationResult]:
"""List all deployed destination connectors in the Airbyte Cloud workspace."""
workspace: CloudWorkspace = _get_cloud_workspace(workspace_id)
destinations = workspace.list_destinations()
# Filter by name if requested
if name_contains:
needle = name_contains.lower()
destinations = [d for d in destinations if d.name is not None and needle in d.name.lower()]
# Apply limit if requested
if max_items_limit is not None:
destinations = destinations[:max_items_limit]
# Note: name and url are guaranteed non-null from list API responses
return [
CloudDestinationResult(
id=destination.destination_id,
name=cast(str, destination.name),
url=cast(str, destination.connector_url),
)
for destination in destinations
]
@mcp_tool(
domain="cloud",
read_only=True,
idempotent=True,
open_world=True,
extra_help_text=CLOUD_AUTH_TIP_TEXT,
)
def describe_cloud_source(
source_id: Annotated[
str,
Field(description="The ID of the source to describe."),
],
*,
workspace_id: Annotated[
str | None,
Field(
description=WORKSPACE_ID_TIP_TEXT,
default=None,
),
],
) -> CloudSourceDetails:
"""Get detailed information about a specific deployed source connector."""
workspace: CloudWorkspace = _get_cloud_workspace(workspace_id)
source = workspace.get_source(source_id=source_id)
# Access name property to ensure _connector_info is populated
source_name = cast(str, source.name)
return CloudSourceDetails(
source_id=source.source_id,
source_name=source_name,
source_url=source.connector_url,
connector_definition_id=source._connector_info.definition_id, # noqa: SLF001 # type: ignore[union-attr]
)
@mcp_tool(
domain="cloud",
read_only=True,
idempotent=True,
open_world=True,
extra_help_text=CLOUD_AUTH_TIP_TEXT,
)
def describe_cloud_destination(
destination_id: Annotated[
str,
Field(description="The ID of the destination to describe."),
],
*,
workspace_id: Annotated[
str | None,
Field(
description=WORKSPACE_ID_TIP_TEXT,
default=None,
),
],
) -> CloudDestinationDetails:
"""Get detailed information about a specific deployed destination connector."""
workspace: CloudWorkspace = _get_cloud_workspace(workspace_id)
destination = workspace.get_destination(destination_id=destination_id)
# Access name property to ensure _connector_info is populated
destination_name = cast(str, destination.name)
return CloudDestinationDetails(
destination_id=destination.destination_id,
destination_name=destination_name,
destination_url=destination.connector_url,
connector_definition_id=destination._connector_info.definition_id, # noqa: SLF001 # type: ignore[union-attr]
)
@mcp_tool(
domain="cloud",
read_only=True,
idempotent=True,
open_world=True,
extra_help_text=CLOUD_AUTH_TIP_TEXT,
)
def describe_cloud_connection(
connection_id: Annotated[
str,
Field(description="The ID of the connection to describe."),
],
*,
workspace_id: Annotated[
str | None,
Field(
description=WORKSPACE_ID_TIP_TEXT,
default=None,
),
],
) -> CloudConnectionDetails:
"""Get detailed information about a specific deployed connection."""
workspace: CloudWorkspace = _get_cloud_workspace(workspace_id)
connection = workspace.get_connection(connection_id=connection_id)
return CloudConnectionDetails(
connection_id=connection.connection_id,
connection_name=cast(str, connection.name),
connection_url=cast(str, connection.connection_url),
source_id=connection.source_id,
source_name=cast(str, connection.source.name),
destination_id=connection.destination_id,
destination_name=cast(str, connection.destination.name),
selected_streams=connection.stream_names,
table_prefix=connection.table_prefix,
)
@mcp_tool(
domain="cloud",
read_only=True,
idempotent=True,
open_world=True,
extra_help_text=CLOUD_AUTH_TIP_TEXT,
)
def get_cloud_sync_logs(
connection_id: Annotated[
str,
Field(description="The ID of the Airbyte Cloud connection."),
],
job_id: Annotated[
int | None,
Field(description="Optional job ID. If not provided, the latest job will be used."),
] = None,
attempt_number: Annotated[
int | None,
Field(
description="Optional attempt number. If not provided, the latest attempt will be used."
),
] = None,
*,
workspace_id: Annotated[
str | None,
Field(
description=WORKSPACE_ID_TIP_TEXT,
default=None,
),
],
max_lines: Annotated[
int,
Field(
description=(
"Maximum number of lines to return. "
"Defaults to 4000 if not specified. "
"If '0' is provided, no limit is applied."
),
default=4000,
),
],
from_tail: Annotated[
bool | None,
Field(
description=(
"Pull from the end of the log text if total lines is greater than 'max_lines'. "
"Defaults to True if `line_offset` is not specified. "
"Cannot combine `from_tail=True` with `line_offset`."
),
default=None,
),
],
line_offset: Annotated[
int | None,
Field(
description=(
"Number of lines to skip from the beginning of the logs. "
"Cannot be combined with `from_tail=True`."
),
default=None,
),
],
) -> LogReadResult:
"""Get the logs from a sync job attempt on Airbyte Cloud."""
# Validate that line_offset and from_tail are not both set
if line_offset is not None and from_tail:
raise PyAirbyteInputError(
message="Cannot specify both 'line_offset' and 'from_tail' parameters.",
context={"line_offset": line_offset, "from_tail": from_tail},
)
if from_tail is None and line_offset is None:
from_tail = True
workspace: CloudWorkspace = _get_cloud_workspace(workspace_id)
connection = workspace.get_connection(connection_id=connection_id)
sync_result: cloud.SyncResult | None = connection.get_sync_result(job_id=job_id)
if not sync_result:
raise AirbyteMissingResourceError(
resource_type="sync job",
resource_name_or_id=connection_id,
)
attempts = sync_result.get_attempts()
if not attempts:
raise AirbyteMissingResourceError(
resource_type="sync attempt",
resource_name_or_id=str(sync_result.job_id),
)
if attempt_number is not None:
target_attempt = None
for attempt in attempts:
if attempt.attempt_number == attempt_number:
target_attempt = attempt
break
if target_attempt is None:
raise AirbyteMissingResourceError(
resource_type="sync attempt",
resource_name_or_id=f"job {sync_result.job_id}, attempt {attempt_number}",
)
else:
target_attempt = max(attempts, key=lambda a: a.attempt_number)
logs = target_attempt.get_full_log_text()
if not logs:
# Return empty result with zero lines
return LogReadResult(
log_text=(
f"[No logs available for job '{sync_result.job_id}', "
f"attempt {target_attempt.attempt_number}.]"
),
log_text_start_line=1,
log_text_line_count=0,
total_log_lines_available=0,
job_id=sync_result.job_id,
attempt_number=target_attempt.attempt_number,
)
# Apply line limiting
log_lines = logs.splitlines()
total_lines = len(log_lines)
# Determine effective max_lines (0 means no limit)
effective_max = total_lines if max_lines == 0 else max_lines
# Calculate start_index and slice based on from_tail or line_offset
if from_tail:
start_index = max(0, total_lines - effective_max)
selected_lines = log_lines[start_index:][:effective_max]
else:
start_index = line_offset or 0
selected_lines = log_lines[start_index : start_index + effective_max]
return LogReadResult(
log_text="\n".join(selected_lines),
log_text_start_line=start_index + 1, # Convert to 1-based index
log_text_line_count=len(selected_lines),
total_log_lines_available=total_lines,
job_id=sync_result.job_id,
attempt_number=target_attempt.attempt_number,
)
@mcp_tool(
domain="cloud",
read_only=True,
idempotent=True,
open_world=True,
extra_help_text=CLOUD_AUTH_TIP_TEXT,
)
def list_deployed_cloud_connections(
*,
workspace_id: Annotated[
str | None,
Field(
description=WORKSPACE_ID_TIP_TEXT,
default=None,
),
],
name_contains: Annotated[
str | None,
Field(
description="Optional case-insensitive substring to filter connections by name",