Skip to content

Commit b890cb8

Browse files
authored
Merge pull request #1313 from Thenujan-Nagaratnam/mcp-server
Fix mcp-server export path
2 parents 09174dd + 058abbe commit b890cb8

File tree

5 files changed

+89
-9
lines changed

5 files changed

+89
-9
lines changed

import-export-cli/cmd/exportMCPServer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func executeExportMCPServerCmd(credential credentials.Credential, exportDirector
8989
utils.Logf(utils.LogPrefixInfo+"ResponseStatus: %v\n", resp.Status())
9090
mcpServerZipLocationPath := filepath.Join(exportDirectory, CmdExportEnvironment)
9191
if resp.StatusCode() == http.StatusOK {
92-
impl.WriteToZip(exportMCPServerName, exportMCPServerVersion, "", mcpServerZipLocationPath, runningExportMCPServerCommand, resp)
92+
impl.WriteMCPServerToZip(exportMCPServerName, exportMCPServerVersion, "", mcpServerZipLocationPath, runningExportMCPServerCommand, resp)
9393
} else if resp.StatusCode() == http.StatusInternalServerError {
9494
// 500 Internal Server Error
9595
fmt.Println(string(resp.Body()))

import-export-cli/cmd/exportMCPServers.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ var ExportMCPServersCmd = &cobra.Command{
6363
// exportDirectory = <export_directory>/migration/
6464
func executeExportMCPServersCmd(credential credentials.Credential, exportDirectory string) {
6565
//create dir structure
66-
mcpServerExportDir := impl.CreateExportAPIsDirStructure(exportDirectory, CmdResourceTenantDomain, CmdExportEnvironment,
66+
mcpServerExportDir := impl.CreateExportMCPServersDirStructure(exportDirectory, CmdResourceTenantDomain, CmdExportEnvironment,
6767
CmdForceStartFromBegin)
6868
exportRelatedFilesPath := filepath.Join(exportDirectory, CmdExportEnvironment,
6969
utils.GetMigrationExportTenantDirName(CmdResourceTenantDomain))

import-export-cli/impl/exportMCPServer.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919
package impl
2020

2121
import (
22+
"fmt"
2223
"net/url"
24+
"path/filepath"
2325
"strconv"
2426

2527
"github.com/go-resty/resty/v2"
@@ -71,3 +73,54 @@ func exportMCPServer(name, version, revisionNum, provider, format, publisherEndp
7173

7274
return resp, nil
7375
}
76+
77+
// WriteMCPServerToZip
78+
// @param exportMCPServerName : Name of the MCP Server to be exported
79+
// @param exportMCPServerVersion: Version of the MCP Server to be exported
80+
// @param exportMCPServerRevisionNumber: Revision number of the MCP Server
81+
// @param zipLocationPath: Path to the export directory
82+
// @param runningExportMCPServerCommand: Whether the export MCP Server command is running
83+
// @param resp : Response returned from making the HTTP request (only pass a 200 OK)
84+
// Exported MCP Server will be written to a zip file
85+
func WriteMCPServerToZip(exportMCPServerName, exportMCPServerVersion, exportMCPServerRevisionNumber, zipLocationPath string,
86+
runningExportMCPServerCommand bool, resp *resty.Response) {
87+
zipFilename := exportMCPServerName + "_" + exportMCPServerVersion
88+
if exportMCPServerRevisionNumber != "" {
89+
zipFilename += "_" + utils.GetRevisionNamFromRevisionNum(exportMCPServerRevisionNumber)
90+
}
91+
zipFilename += ".zip" // MyMCPServer_1.0.0_Revision-1.zip
92+
// Writes the REST MCP Server response to a temporary zip file
93+
tempZipFile, err := utils.WriteResponseToTempZip(zipFilename, resp)
94+
if err != nil {
95+
utils.HandleErrorAndExit("Error creating the temporary zip file to store the exported MCP Server", err)
96+
}
97+
98+
err = utils.CreateDirIfNotExist(zipLocationPath)
99+
if err != nil {
100+
utils.HandleErrorAndExit("Error creating dir to store zip archive: "+zipLocationPath, err)
101+
}
102+
exportedFinalZip := filepath.Join(zipLocationPath, zipFilename)
103+
104+
// Add mcp_server_meta.yaml file inside the zip and create a new zup file in exportedFinalZip location
105+
metaData := utils.MetaData{
106+
Name: exportMCPServerName,
107+
Version: exportMCPServerVersion,
108+
DeployConfig: utils.DeployConfig{
109+
Import: utils.ImportConfig{
110+
Update: true,
111+
PreserveProvider: true,
112+
RotateRevision: false,
113+
},
114+
},
115+
}
116+
err = IncludeMetaFileToZip(tempZipFile, exportedFinalZip, utils.MetaFileMCPServer, metaData)
117+
if err != nil {
118+
utils.HandleErrorAndExit("Error creating the final zip archive with mcp_server_meta.yaml file", err)
119+
}
120+
121+
// Output the final zip file location.
122+
if runningExportMCPServerCommand {
123+
fmt.Println("Successfully exported MCP Server!")
124+
fmt.Println("Find the exported MCP Server at " + exportedFinalZip)
125+
}
126+
}

import-export-cli/impl/exportMCPServers.go

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,11 @@ import (
2929
"github.com/wso2/product-apim-tooling/import-export-cli/utils"
3030
)
3131

32-
var mcpServerExportDir string
3332
var mcpServerListOffset int //from which index of MCP Server, the MCP Servers will be fetched from APIM server
3433
var mcpServerCount int32 // size of MCP Server list to be exported or number of MCP Servers left to be exported from last iteration
3534
var mcpServers []utils.MCPServer
36-
var exportMCPServersRelatedFilesPath string
37-
var exportMCPServersFormat string
3835

3936
var startingMCPServerIndexFromList int
40-
var mcpServerMainConfigFilePath string
4137

4238
// Prepare resumption of previous-halted export-mcp-servers operation
4339
func PrepareMCPServerResumption(credential credentials.Credential, exportRelatedFilesPath, cmdResourceTenantDomain, cmdUsername, cmdExportEnvironment string) {
@@ -145,7 +141,7 @@ func getRevisionsListForMCPServer(accessToken, environment string, mcpServer uti
145141

146142
// Do the MCP Server exportation
147143
func ExportMCPServers(credential credentials.Credential, exportRelatedFilesPath, environment, tenantDomain, format,
148-
username, exportDir string, preserveStatus, runningExportMCPServerCommand, allRevisions,
144+
username, mcpServerExportDir string, preserveStatus, runningExportMCPServerCommand, allRevisions,
149145
preserveCredentials bool) {
150146

151147
if mcpServerCount == 0 {
@@ -196,7 +192,7 @@ func ExportMCPServers(credential credentials.Credential, exportRelatedFilesPath,
196192
}
197193
}
198194
fmt.Println("\nTotal number of MCP Servers exported: " + cast.ToString(counterSucceededMCPServers))
199-
fmt.Println("MCP Server export path: " + exportDir)
195+
fmt.Println("MCP Server export path: " + mcpServerExportDir)
200196
fmt.Println("\nCommand: export-mcp-servers execution completed !")
201197
}
202198
}
@@ -222,11 +218,41 @@ func exportMCPServerAndWriteToZip(mcpServer utils.MCPServer, revisionNumber, acc
222218

223219
if resp.StatusCode() == http.StatusOK {
224220
utils.Logf(utils.LogPrefixInfo+"ResponseStatus: %v\n", resp.Status())
225-
WriteToZip(exportMCPServerName, exportMCPServerVersion, exportMCPServerRevision, mcpServerExportDir, runningExportMCPServerCommand, resp)
221+
WriteMCPServerToZip(exportMCPServerName, exportMCPServerVersion, exportMCPServerRevision, mcpServerExportDir, runningExportMCPServerCommand, resp)
226222
//write on last-succeeded-mcp-server.log
227223
utils.WriteLastSucceededMCPServerFileData(exportRelatedFilesPath, mcpServer)
228224
} else {
229225
fmt.Printf("Error exporting MCP Server: %s - %s of Provider: %s\n", exportMCPServerName, exportMCPServerVersion, exportMCPServerProvider)
230226
utils.PrintErrorResponseAndExit(resp)
231227
}
232228
}
229+
230+
// Create the required directory structure to save the exported MCP Servers
231+
func CreateExportMCPServersDirStructure(artifactExportDirectory, cmdResourceTenantDomain, cmdExportEnvironment string, cmdForceStartFromBegin bool) string {
232+
var resourceTenantDirName = utils.GetMigrationExportTenantDirName(cmdResourceTenantDomain)
233+
234+
var createDirError error
235+
createDirError = utils.CreateDirIfNotExist(artifactExportDirectory)
236+
237+
migrationsArtifactsEnvPath := filepath.Join(artifactExportDirectory, cmdExportEnvironment)
238+
migrationsArtifactsEnvTenantPath := filepath.Join(migrationsArtifactsEnvPath, resourceTenantDirName)
239+
migrationsArtifactsEnvTenantMCPServersPath := filepath.Join(migrationsArtifactsEnvTenantPath, utils.ExportedMCPServersDirName)
240+
241+
createDirError = utils.CreateDirIfNotExist(migrationsArtifactsEnvPath)
242+
createDirError = utils.CreateDirIfNotExist(migrationsArtifactsEnvTenantPath)
243+
244+
if dirExists, _ := utils.IsDirExists(migrationsArtifactsEnvTenantMCPServersPath); dirExists {
245+
if cmdForceStartFromBegin {
246+
utils.RemoveDirectory(migrationsArtifactsEnvTenantMCPServersPath)
247+
createDirError = utils.CreateDir(migrationsArtifactsEnvTenantMCPServersPath)
248+
}
249+
} else {
250+
createDirError = utils.CreateDir(migrationsArtifactsEnvTenantMCPServersPath)
251+
}
252+
253+
if createDirError != nil {
254+
utils.HandleErrorAndExit("Error in creating directory structure for the MCP Server export for migration .",
255+
createDirError)
256+
}
257+
return migrationsArtifactsEnvTenantMCPServersPath
258+
}

import-export-cli/utils/constants.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,7 @@ const (
272272
// project meta files
273273
const (
274274
MetaFileAPI = "api_meta.yaml"
275+
MetaFileMCPServer = "mcp_server_meta.yaml"
275276
MetaFileAPIProduct = "api_product_meta.yaml"
276277
MetaFileApplication = "application_meta.yaml"
277278
)

0 commit comments

Comments
 (0)