@azure-devops/mcp 0.1.0 → 0.2.0-preview-oauth

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.
@@ -6,28 +6,53 @@ const WIKI_TOOLS = {
6
6
  get_wiki: "wiki_get_wiki",
7
7
  list_wiki_pages: "wiki_list_pages",
8
8
  get_wiki_page_content: "wiki_get_page_content",
9
+ create_or_update_page: "wiki_create_or_update_page",
9
10
  };
10
- function configureWikiTools(server, tokenProvider, connectionProvider) {
11
+ function configureWikiTools(server, tokenProvider, connectionProvider, userAgentProvider) {
11
12
  server.tool(WIKI_TOOLS.get_wiki, "Get the wiki by wikiIdentifier", {
12
13
  wikiIdentifier: z.string().describe("The unique identifier of the wiki."),
13
14
  project: z.string().optional().describe("The project name or ID where the wiki is located. If not provided, the default project will be used."),
14
15
  }, async ({ wikiIdentifier, project }) => {
15
- const connection = await connectionProvider();
16
- const wikiApi = await connection.getWikiApi();
17
- const wiki = await wikiApi.getWiki(wikiIdentifier, project);
18
- return {
19
- content: [{ type: "text", text: JSON.stringify(wiki, null, 2) }],
20
- };
16
+ try {
17
+ const connection = await connectionProvider();
18
+ const wikiApi = await connection.getWikiApi();
19
+ const wiki = await wikiApi.getWiki(wikiIdentifier, project);
20
+ if (!wiki) {
21
+ return { content: [{ type: "text", text: "No wiki found" }], isError: true };
22
+ }
23
+ return {
24
+ content: [{ type: "text", text: JSON.stringify(wiki, null, 2) }],
25
+ };
26
+ }
27
+ catch (error) {
28
+ const errorMessage = error instanceof Error ? error.message : "Unknown error occurred";
29
+ return {
30
+ content: [{ type: "text", text: `Error fetching wiki: ${errorMessage}` }],
31
+ isError: true,
32
+ };
33
+ }
21
34
  });
22
35
  server.tool(WIKI_TOOLS.list_wikis, "Retrieve a list of wikis for an organization or project.", {
23
36
  project: z.string().optional().describe("The project name or ID to filter wikis. If not provided, all wikis in the organization will be returned."),
24
37
  }, async ({ project }) => {
25
- const connection = await connectionProvider();
26
- const wikiApi = await connection.getWikiApi();
27
- const wikis = await wikiApi.getAllWikis(project);
28
- return {
29
- content: [{ type: "text", text: JSON.stringify(wikis, null, 2) }],
30
- };
38
+ try {
39
+ const connection = await connectionProvider();
40
+ const wikiApi = await connection.getWikiApi();
41
+ const wikis = await wikiApi.getAllWikis(project);
42
+ if (!wikis) {
43
+ return { content: [{ type: "text", text: "No wikis found" }], isError: true };
44
+ }
45
+ return {
46
+ content: [{ type: "text", text: JSON.stringify(wikis, null, 2) }],
47
+ };
48
+ }
49
+ catch (error) {
50
+ const errorMessage = error instanceof Error ? error.message : "Unknown error occurred";
51
+ return {
52
+ content: [{ type: "text", text: `Error fetching wikis: ${errorMessage}` }],
53
+ isError: true,
54
+ };
55
+ }
31
56
  });
32
57
  server.tool(WIKI_TOOLS.list_wiki_pages, "Retrieve a list of wiki pages for a specific wiki and project.", {
33
58
  wikiIdentifier: z.string().describe("The unique identifier of the wiki."),
@@ -35,31 +60,221 @@ function configureWikiTools(server, tokenProvider, connectionProvider) {
35
60
  top: z.number().default(20).describe("The maximum number of pages to return. Defaults to 20."),
36
61
  continuationToken: z.string().optional().describe("Token for pagination to retrieve the next set of pages."),
37
62
  pageViewsForDays: z.number().optional().describe("Number of days to retrieve page views for. If not specified, page views are not included."),
38
- }, async ({ wikiIdentifier, project, top = 20, continuationToken, pageViewsForDays, }) => {
39
- const connection = await connectionProvider();
40
- const wikiApi = await connection.getWikiApi();
41
- const pagesBatchRequest = {
42
- top,
43
- continuationToken,
44
- pageViewsForDays,
45
- };
46
- const pages = await wikiApi.getPagesBatch(pagesBatchRequest, project, wikiIdentifier);
47
- return {
48
- content: [{ type: "text", text: JSON.stringify(pages, null, 2) }],
49
- };
63
+ }, async ({ wikiIdentifier, project, top = 20, continuationToken, pageViewsForDays }) => {
64
+ try {
65
+ const connection = await connectionProvider();
66
+ const wikiApi = await connection.getWikiApi();
67
+ const pagesBatchRequest = {
68
+ top,
69
+ continuationToken,
70
+ pageViewsForDays,
71
+ };
72
+ const pages = await wikiApi.getPagesBatch(pagesBatchRequest, project, wikiIdentifier);
73
+ if (!pages) {
74
+ return { content: [{ type: "text", text: "No wiki pages found" }], isError: true };
75
+ }
76
+ return {
77
+ content: [{ type: "text", text: JSON.stringify(pages, null, 2) }],
78
+ };
79
+ }
80
+ catch (error) {
81
+ const errorMessage = error instanceof Error ? error.message : "Unknown error occurred";
82
+ return {
83
+ content: [{ type: "text", text: `Error fetching wiki pages: ${errorMessage}` }],
84
+ isError: true,
85
+ };
86
+ }
50
87
  });
51
- server.tool(WIKI_TOOLS.get_wiki_page_content, "Retrieve wiki page content by wikiIdentifier and path.", {
52
- wikiIdentifier: z.string().describe("The unique identifier of the wiki."),
53
- project: z.string().describe("The project name or ID where the wiki is located."),
54
- path: z.string().describe("The path of the wiki page to retrieve content for."),
55
- }, async ({ wikiIdentifier, project, path }) => {
56
- const connection = await connectionProvider();
57
- const wikiApi = await connection.getWikiApi();
58
- const stream = await wikiApi.getPageText(project, wikiIdentifier, path, undefined, undefined, true);
59
- const content = await streamToString(stream);
60
- return {
61
- content: [{ type: "text", text: JSON.stringify(content, null, 2) }],
62
- };
88
+ server.tool(WIKI_TOOLS.get_wiki_page_content, "Retrieve wiki page content. Provide either a 'url' parameter OR the combination of 'wikiIdentifier' and 'project' parameters.", {
89
+ url: z
90
+ .string()
91
+ .optional()
92
+ .describe("The full URL of the wiki page to retrieve content for. If provided, wikiIdentifier, project, and path are ignored. Supported patterns: https://dev.azure.com/{org}/{project}/_wiki/wikis/{wikiIdentifier}?pagePath=%2FMy%20Page and https://dev.azure.com/{org}/{project}/_wiki/wikis/{wikiIdentifier}/{pageId}/Page-Title"),
93
+ wikiIdentifier: z.string().optional().describe("The unique identifier of the wiki. Required if url is not provided."),
94
+ project: z.string().optional().describe("The project name or ID where the wiki is located. Required if url is not provided."),
95
+ path: z.string().optional().describe("The path of the wiki page to retrieve content for. Optional, defaults to root page if not provided."),
96
+ }, async ({ url, wikiIdentifier, project, path }) => {
97
+ try {
98
+ const hasUrl = !!url;
99
+ const hasPair = !!wikiIdentifier && !!project;
100
+ if (hasUrl && hasPair) {
101
+ return { content: [{ type: "text", text: "Error fetching wiki page content: Provide either 'url' OR 'wikiIdentifier' with 'project', not both." }], isError: true };
102
+ }
103
+ if (!hasUrl && !hasPair) {
104
+ return { content: [{ type: "text", text: "Error fetching wiki page content: You must provide either 'url' OR both 'wikiIdentifier' and 'project'." }], isError: true };
105
+ }
106
+ const connection = await connectionProvider();
107
+ const wikiApi = await connection.getWikiApi();
108
+ let resolvedProject = project;
109
+ let resolvedWiki = wikiIdentifier;
110
+ let resolvedPath = path;
111
+ let pageContent;
112
+ if (url) {
113
+ const parsed = parseWikiUrl(url);
114
+ if ("error" in parsed) {
115
+ return { content: [{ type: "text", text: `Error fetching wiki page content: ${parsed.error}` }], isError: true };
116
+ }
117
+ resolvedProject = parsed.project;
118
+ resolvedWiki = parsed.wikiIdentifier;
119
+ if (parsed.pagePath) {
120
+ resolvedPath = parsed.pagePath;
121
+ }
122
+ if (parsed.pageId) {
123
+ try {
124
+ const accessToken = await tokenProvider();
125
+ const baseUrl = connection.serverUrl.replace(/\/$/, "");
126
+ const restUrl = `${baseUrl}/${resolvedProject}/_apis/wiki/wikis/${resolvedWiki}/pages/${parsed.pageId}?includeContent=true&api-version=7.1`;
127
+ const resp = await fetch(restUrl, {
128
+ headers: {
129
+ "Authorization": `Bearer ${accessToken}`,
130
+ "User-Agent": userAgentProvider(),
131
+ },
132
+ });
133
+ if (resp.ok) {
134
+ const json = await resp.json();
135
+ if (json && typeof json.content === "string") {
136
+ pageContent = json.content;
137
+ }
138
+ else if (json && json.path) {
139
+ resolvedPath = json.path;
140
+ }
141
+ }
142
+ else if (resp.status === 404) {
143
+ return { content: [{ type: "text", text: `Error fetching wiki page content: Page with id ${parsed.pageId} not found` }], isError: true };
144
+ }
145
+ }
146
+ catch { }
147
+ }
148
+ }
149
+ if (!pageContent) {
150
+ if (!resolvedPath) {
151
+ resolvedPath = "/";
152
+ }
153
+ if (!resolvedProject || !resolvedWiki) {
154
+ return { content: [{ type: "text", text: "Project and wikiIdentifier must be defined to fetch wiki page content." }], isError: true };
155
+ }
156
+ const stream = await wikiApi.getPageText(resolvedProject, resolvedWiki, resolvedPath, undefined, undefined, true);
157
+ if (!stream) {
158
+ return { content: [{ type: "text", text: "No wiki page content found" }], isError: true };
159
+ }
160
+ pageContent = await streamToString(stream);
161
+ }
162
+ return { content: [{ type: "text", text: JSON.stringify(pageContent, null, 2) }] };
163
+ }
164
+ catch (error) {
165
+ const errorMessage = error instanceof Error ? error.message : "Unknown error occurred";
166
+ return {
167
+ content: [{ type: "text", text: `Error fetching wiki page content: ${errorMessage}` }],
168
+ isError: true,
169
+ };
170
+ }
171
+ });
172
+ server.tool(WIKI_TOOLS.create_or_update_page, "Create or update a wiki page with content.", {
173
+ wikiIdentifier: z.string().describe("The unique identifier or name of the wiki."),
174
+ path: z.string().describe("The path of the wiki page (e.g., '/Home' or '/Documentation/Setup')."),
175
+ content: z.string().describe("The content of the wiki page in markdown format."),
176
+ project: z.string().optional().describe("The project name or ID where the wiki is located. If not provided, the default project will be used."),
177
+ etag: z.string().optional().describe("ETag for editing existing pages (optional, will be fetched if not provided)."),
178
+ }, async ({ wikiIdentifier, path, content, project, etag }) => {
179
+ try {
180
+ const connection = await connectionProvider();
181
+ const accessToken = await tokenProvider();
182
+ // Normalize the path
183
+ const normalizedPath = path.startsWith("/") ? path : `/${path}`;
184
+ const encodedPath = encodeURIComponent(normalizedPath);
185
+ // Build the URL for the wiki page API
186
+ const baseUrl = connection.serverUrl;
187
+ const projectParam = project || "";
188
+ const url = `${baseUrl}/${projectParam}/_apis/wiki/wikis/${wikiIdentifier}/pages?path=${encodedPath}&api-version=7.1`;
189
+ // First, try to create a new page (PUT without ETag)
190
+ try {
191
+ const createResponse = await fetch(url, {
192
+ method: "PUT",
193
+ headers: {
194
+ "Authorization": `Bearer ${accessToken}`,
195
+ "Content-Type": "application/json",
196
+ "User-Agent": userAgentProvider(),
197
+ },
198
+ body: JSON.stringify({ content: content }),
199
+ });
200
+ if (createResponse.ok) {
201
+ const result = await createResponse.json();
202
+ return {
203
+ content: [
204
+ {
205
+ type: "text",
206
+ text: `Successfully created wiki page at path: ${normalizedPath}. Response: ${JSON.stringify(result, null, 2)}`,
207
+ },
208
+ ],
209
+ };
210
+ }
211
+ // If creation failed with 409 (Conflict) or 500 (Page exists), try to update it
212
+ if (createResponse.status === 409 || createResponse.status === 500) {
213
+ // Page exists, we need to get the ETag and update it
214
+ let currentEtag = etag;
215
+ if (!currentEtag) {
216
+ // Fetch current page to get ETag
217
+ const getResponse = await fetch(url, {
218
+ method: "GET",
219
+ headers: {
220
+ "Authorization": `Bearer ${accessToken}`,
221
+ "User-Agent": userAgentProvider(),
222
+ },
223
+ });
224
+ if (getResponse.ok) {
225
+ currentEtag = getResponse.headers.get("etag") || getResponse.headers.get("ETag") || undefined;
226
+ if (!currentEtag) {
227
+ const pageData = await getResponse.json();
228
+ currentEtag = pageData.eTag;
229
+ }
230
+ }
231
+ if (!currentEtag) {
232
+ throw new Error("Could not retrieve ETag for existing page");
233
+ }
234
+ }
235
+ // Now update the existing page with ETag
236
+ const updateResponse = await fetch(url, {
237
+ method: "PUT",
238
+ headers: {
239
+ "Authorization": `Bearer ${accessToken}`,
240
+ "Content-Type": "application/json",
241
+ "User-Agent": userAgentProvider(),
242
+ "If-Match": currentEtag,
243
+ },
244
+ body: JSON.stringify({ content: content }),
245
+ });
246
+ if (updateResponse.ok) {
247
+ const result = await updateResponse.json();
248
+ return {
249
+ content: [
250
+ {
251
+ type: "text",
252
+ text: `Successfully updated wiki page at path: ${normalizedPath}. Response: ${JSON.stringify(result, null, 2)}`,
253
+ },
254
+ ],
255
+ };
256
+ }
257
+ else {
258
+ const errorText = await updateResponse.text();
259
+ throw new Error(`Failed to update page (${updateResponse.status}): ${errorText}`);
260
+ }
261
+ }
262
+ else {
263
+ const errorText = await createResponse.text();
264
+ throw new Error(`Failed to create page (${createResponse.status}): ${errorText}`);
265
+ }
266
+ }
267
+ catch (fetchError) {
268
+ throw fetchError;
269
+ }
270
+ }
271
+ catch (error) {
272
+ const errorMessage = error instanceof Error ? error.message : "Unknown error occurred";
273
+ return {
274
+ content: [{ type: "text", text: `Error creating/updating wiki page: ${errorMessage}` }],
275
+ isError: true,
276
+ };
277
+ }
63
278
  });
64
279
  }
65
280
  function streamToString(stream) {
@@ -71,4 +286,47 @@ function streamToString(stream) {
71
286
  stream.on("error", reject);
72
287
  });
73
288
  }
289
+ // Helper to parse Azure DevOps wiki page URLs.
290
+ // Supported examples:
291
+ // - https://dev.azure.com/org/project/_wiki/wikis/wikiIdentifier?wikiVersion=GBmain&pagePath=%2FHome
292
+ // - https://dev.azure.com/org/project/_wiki/wikis/wikiIdentifier/123/Title-Of-Page
293
+ // Returns either a structured object OR an error message inside { error }.
294
+ function parseWikiUrl(url) {
295
+ try {
296
+ const u = new URL(url);
297
+ // Path segments after host
298
+ // Expect pattern: /{project}/_wiki/wikis/{wikiIdentifier}[/{pageId}/...]
299
+ const segments = u.pathname.split("/").filter(Boolean); // remove empty
300
+ const idx = segments.findIndex((s) => s === "_wiki");
301
+ if (idx < 1 || segments[idx + 1] !== "wikis") {
302
+ return { error: "URL does not match expected wiki pattern (missing /_wiki/wikis/ segment)." };
303
+ }
304
+ const project = segments[idx - 1];
305
+ const wikiIdentifier = segments[idx + 2];
306
+ if (!project || !wikiIdentifier) {
307
+ return { error: "Could not extract project or wikiIdentifier from URL." };
308
+ }
309
+ // Query form with pagePath
310
+ const pagePathParam = u.searchParams.get("pagePath");
311
+ if (pagePathParam) {
312
+ let decoded = decodeURIComponent(pagePathParam);
313
+ if (!decoded.startsWith("/"))
314
+ decoded = "/" + decoded;
315
+ return { project, wikiIdentifier, pagePath: decoded };
316
+ }
317
+ // Path ID form: .../wikis/{wikiIdentifier}/{pageId}/...
318
+ const afterWiki = segments.slice(idx + 3); // elements after wikiIdentifier
319
+ if (afterWiki.length >= 1) {
320
+ const maybeId = parseInt(afterWiki[0], 10);
321
+ if (!isNaN(maybeId)) {
322
+ return { project, wikiIdentifier, pageId: maybeId };
323
+ }
324
+ }
325
+ // If nothing else specified, treat as root page
326
+ return { project, wikiIdentifier, pagePath: "/" };
327
+ }
328
+ catch {
329
+ return { error: "Invalid URL format." };
330
+ }
331
+ }
74
332
  export { WIKI_TOOLS, configureWikiTools };