@@ -13,6 +13,31 @@ import { createApiClient, type Schemas } from "./generated";
1313
1414const log = logger . scope ( "posthog-client" ) ;
1515
16+ export interface McpRecommendedServer {
17+ name : string ;
18+ url : string ;
19+ description : string ;
20+ icon_url : string ;
21+ auth_type : "none" | "api_key" | "oauth" ;
22+ oauth_provider_kind ?: string ;
23+ }
24+
25+ export interface McpServerInstallation {
26+ id : string ;
27+ server_id : string | null ;
28+ name : string ;
29+ display_name : string ;
30+ url : string ;
31+ description : string ;
32+ auth_type : "none" | "api_key" | "oauth" ;
33+ configuration : Record < string , unknown > ;
34+ needs_reauth : boolean ;
35+ pending_oauth : boolean ;
36+ proxy_url : string ;
37+ created_at : string ;
38+ updated_at : string ;
39+ }
40+
1641function isObjectRecord ( value : unknown ) : value is Record < string , unknown > {
1742 return typeof value === "object" && value !== null ;
1843}
@@ -766,6 +791,94 @@ export class PostHogAPIClient {
766791 } as RepoAutonomyStatus ;
767792 }
768793
794+ async getMcpServers ( ) : Promise < McpRecommendedServer [ ] > {
795+ const teamId = await this . getTeamId ( ) ;
796+ const url = new URL (
797+ `${ this . api . baseUrl } /api/environments/${ teamId } /mcp_servers/` ,
798+ ) ;
799+ const response = await this . api . fetcher . fetch ( {
800+ method : "get" ,
801+ url,
802+ path : `/api/environments/${ teamId } /mcp_servers/` ,
803+ } ) ;
804+
805+ if ( ! response . ok ) {
806+ throw new Error ( `Failed to fetch MCP servers: ${ response . statusText } ` ) ;
807+ }
808+
809+ const data = await response . json ( ) ;
810+ return data . results ?? data ?? [ ] ;
811+ }
812+
813+ async getMcpServerInstallations ( ) : Promise < McpServerInstallation [ ] > {
814+ const teamId = await this . getTeamId ( ) ;
815+ const url = new URL (
816+ `${ this . api . baseUrl } /api/environments/${ teamId } /mcp_server_installations/` ,
817+ ) ;
818+ const response = await this . api . fetcher . fetch ( {
819+ method : "get" ,
820+ url,
821+ path : `/api/environments/${ teamId } /mcp_server_installations/` ,
822+ } ) ;
823+
824+ if ( ! response . ok ) {
825+ throw new Error (
826+ `Failed to fetch MCP server installations: ${ response . statusText } ` ,
827+ ) ;
828+ }
829+
830+ const data = await response . json ( ) ;
831+ return data . results ?? data ?? [ ] ;
832+ }
833+
834+ async installCustomMcpServer ( options : {
835+ name : string ;
836+ url : string ;
837+ auth_type : "none" | "api_key" | "oauth" ;
838+ api_key ?: string ;
839+ description ?: string ;
840+ oauth_provider_kind ?: string ;
841+ } ) : Promise < McpServerInstallation & { redirect_url ?: string } > {
842+ const teamId = await this . getTeamId ( ) ;
843+ const apiUrl = new URL (
844+ `${ this . api . baseUrl } /api/environments/${ teamId } /mcp_server_installations/install_custom/` ,
845+ ) ;
846+ const response = await this . api . fetcher . fetch ( {
847+ method : "post" ,
848+ url : apiUrl ,
849+ path : `/api/environments/${ teamId } /mcp_server_installations/install_custom/` ,
850+ overrides : {
851+ body : JSON . stringify ( options ) ,
852+ } ,
853+ } ) ;
854+
855+ if ( ! response . ok ) {
856+ const errorData = await response . json ( ) . catch ( ( ) => ( { } ) ) ;
857+ throw new Error (
858+ ( errorData as { detail ?: string } ) . detail ??
859+ `Failed to install MCP server: ${ response . statusText } ` ,
860+ ) ;
861+ }
862+
863+ return await response . json ( ) ;
864+ }
865+
866+ async uninstallMcpServer ( installationId : string ) : Promise < void > {
867+ const teamId = await this . getTeamId ( ) ;
868+ const url = new URL (
869+ `${ this . api . baseUrl } /api/environments/${ teamId } /mcp_server_installations/${ installationId } /` ,
870+ ) ;
871+ const response = await this . api . fetcher . fetch ( {
872+ method : "delete" ,
873+ url,
874+ path : `/api/environments/${ teamId } /mcp_server_installations/${ installationId } /` ,
875+ } ) ;
876+
877+ if ( ! response . ok && response . status !== 204 ) {
878+ throw new Error ( `Failed to uninstall MCP server: ${ response . statusText } ` ) ;
879+ }
880+ }
881+
769882 /**
770883 * Check if a feature flag is enabled for the current project.
771884 * Returns true if the flag exists and is active, false otherwise.
0 commit comments