1
- import axios , { AxiosError } from "axios" ;
1
+ import axios from "axios" ;
2
2
import config from "../../config" ;
3
3
import { z } from "zod" ;
4
+ import { CallToolResult } from "@modelcontextprotocol/sdk/types.js" ;
5
+ import { formatAxiosError } from "../../lib/error" ; // or correct
4
6
5
7
interface TestCaseStep {
6
8
step : string ;
@@ -70,18 +72,15 @@ export const CreateTestCaseSchema = z.object({
70
72
description : z
71
73
. string ( )
72
74
. optional ( )
73
- . nullish ( )
74
75
. describe ( "Brief description of the test case." ) ,
75
76
owner : z
76
77
. string ( )
77
78
. email ( )
78
79
. describe ( "Email of the test case owner." )
79
- . optional ( )
80
- . nullish ( ) ,
80
+ . optional ( ) ,
81
81
preconditions : z
82
82
. string ( )
83
83
. optional ( )
84
- . nullish ( )
85
84
. describe ( "Any preconditions (HTML allowed)." ) ,
86
85
test_case_steps : z
87
86
. array (
@@ -101,15 +100,10 @@ export const CreateTestCaseSchema = z.object({
101
100
. object ( {
102
101
name : z
103
102
. string ( )
104
- . nullish ( )
105
103
. describe (
106
- "Issue tracker name, For example, use jira for Jira, azure for Azure DevOps, or asana for Asana " ,
104
+ "Issue tracker name, For example, use jira for Jira, azure for Azure DevOps, or asana for Asana. " ,
107
105
) ,
108
- host : z
109
- . string ( )
110
- . url ( )
111
- . describe ( "Base URL of the issue tracker." )
112
- . nullish ( ) ,
106
+ host : z . string ( ) . url ( ) . describe ( "Base URL of the issue tracker." ) ,
113
107
} )
114
108
. optional ( ) ,
115
109
tags : z
@@ -145,68 +139,52 @@ export function sanitizeArgs(args: any) {
145
139
146
140
export async function createTestCase (
147
141
params : TestCaseCreateRequest ,
148
- ) : Promise < TestCaseResponse > {
149
- const {
150
- project_identifier,
151
- folder_id,
152
- name,
153
- description,
154
- owner,
155
- preconditions,
156
- test_case_steps,
157
- issues,
158
- issue_tracker,
159
- tags,
160
- custom_fields,
161
- } = params ;
162
-
163
- const body = {
164
- test_case : {
165
- name,
166
- description,
167
- owner,
168
- preconditions,
169
- test_case_steps,
170
- issues,
171
- issue_tracker,
172
- tags,
173
- custom_fields,
174
- } ,
175
- } ;
142
+ ) : Promise < CallToolResult > {
143
+ const body = { test_case : params } ;
176
144
177
145
try {
178
146
const response = await axios . post < TestCaseResponse > (
179
- `https://test-management.browserstack.com/api/v2/projects/${ project_identifier } /folders/${ folder_id } /test-cases` ,
147
+ `https://test-management.browserstack.com/api/v2/projects/${ encodeURIComponent (
148
+ params . project_identifier ,
149
+ ) } /folders/${ encodeURIComponent ( params . folder_id ) } /test-cases`,
180
150
body ,
181
151
{
182
152
auth : {
183
153
username : config . browserstackUsername ,
184
154
password : config . browserstackAccessKey ,
185
155
} ,
186
- headers : {
187
- "Content-Type" : "application/json" ,
188
- } ,
156
+ headers : { "Content-Type" : "application/json" } ,
189
157
} ,
190
158
) ;
191
159
192
- // Check if the response indicates success
193
- if ( ! response . data . data . success ) {
194
- throw new Error (
195
- `Failed to create test case: ${ JSON . stringify ( response . data ) } ` ,
196
- ) ;
160
+ const { data } = response . data ;
161
+ if ( ! data . success ) {
162
+ return {
163
+ content : [
164
+ {
165
+ type : "text" ,
166
+ text : `Failed to create test case: ${ JSON . stringify (
167
+ response . data ,
168
+ ) } `,
169
+ isError : true ,
170
+ } ,
171
+ ] ,
172
+ isError : true ,
173
+ } ;
197
174
}
198
175
199
- return response . data ;
200
- } catch ( error ) {
201
- if ( error instanceof AxiosError ) {
202
- if ( error . response ?. data ?. message ) {
203
- throw new Error (
204
- `Failed to create test case: ${ error . response . data . message } ` ,
205
- ) ;
206
- } else {
207
- throw new Error ( `Failed to create test case: ${ error . message } ` ) ;
208
- }
209
- }
210
- throw error ;
176
+ const tc = data . test_case ;
177
+ return {
178
+ content : [
179
+ {
180
+ type : "text" ,
181
+ text : `Successfully created test case ${ tc . identifier } : ${ tc . title } ` ,
182
+ } ,
183
+ { type : "text" , text : JSON . stringify ( tc , null , 2 ) } ,
184
+ ] ,
185
+ } ;
186
+ } catch ( err ) {
187
+ // Delegate to our centralized Axios error formatter
188
+ return formatAxiosError ( err , "Failed to create test case" ) ;
211
189
}
212
190
}
0 commit comments