@@ -34,7 +34,9 @@ import {
3434 PullRequestHostRequiredError ,
3535 PullRequestUnlinkFailedError ,
3636 PullRequestListFailedError ,
37+ PullRequestNotOpenError ,
3738 type PullRequestTargetInput ,
39+ PullRequestWatchFailedError ,
3840 PullRequestThreadNotFoundError ,
3941 PullRequestsToolkit ,
4042 type ThreadPullRequestEntry ,
@@ -122,6 +124,7 @@ function entryOf(
122124 number : link . number ,
123125 url : link . url ,
124126 source : link . source ,
127+ watching : link . watch !== undefined ,
125128 state : link . snapshot ?. state ?? null ,
126129 title : link . snapshot ?. title ?? null ,
127130 headBranch : link . snapshot ?. headBranch ?? null ,
@@ -163,7 +166,8 @@ const make = Effect.gen(function* () {
163166 Failure :
164167 | typeof PullRequestLinkFailedError
165168 | typeof PullRequestUnlinkFailedError
166- | typeof PullRequestListFailedError ,
169+ | typeof PullRequestListFailedError
170+ | typeof PullRequestWatchFailedError ,
167171 ) {
168172 const scope = yield * McpInvocationContext . requireMcpCapability ( "pull-requests" ) ;
169173 const thread = yield * engine
@@ -178,22 +182,76 @@ const make = Effect.gen(function* () {
178182
179183 const projectOf = (
180184 thread : OrchestrationV2ThreadShell ,
181- Failure : typeof PullRequestLinkFailedError | typeof PullRequestUnlinkFailedError ,
185+ Failure :
186+ | typeof PullRequestLinkFailedError
187+ | typeof PullRequestUnlinkFailedError
188+ | typeof PullRequestWatchFailedError ,
182189 ) =>
183190 projects . getShell ( thread . projectId ) . pipe (
184191 Effect . map ( Option . getOrUndefined ) ,
185192 Effect . mapError ( ( cause ) => new Failure ( { cause } ) ) ,
186193 ) ;
187194
188195 const dispatchFailure =
189- ( Failure : typeof PullRequestLinkFailedError | typeof PullRequestUnlinkFailedError ) =>
196+ (
197+ Failure :
198+ | typeof PullRequestLinkFailedError
199+ | typeof PullRequestUnlinkFailedError
200+ | typeof PullRequestWatchFailedError ,
201+ ) =>
190202 < E > (
191203 cause : Cause . Cause < E > ,
192- ) : Effect . Effect < never , PullRequestLinkFailedError | PullRequestUnlinkFailedError > =>
204+ ) : Effect . Effect <
205+ never ,
206+ PullRequestLinkFailedError | PullRequestUnlinkFailedError | PullRequestWatchFailedError
207+ > =>
193208 Cause . hasInterruptsOnly ( cause )
194209 ? Effect . failCause ( cause as Cause . Cause < never > )
195210 : Effect . fail ( new Failure ( { cause } ) ) ;
196211
212+ /**
213+ * Starts or stops a watch. One command links an unlinked pull request and watches it, and the
214+ * result reports the state the thread holds afterwards.
215+ */
216+ const setWatching = Effect . fn ( "PullRequestsToolkit.setWatching" ) ( function * (
217+ input : PullRequestTargetInput ,
218+ watching : boolean ,
219+ ) {
220+ const thread = yield * requireThread ( PullRequestWatchFailedError ) ;
221+ const project = yield * projectOf ( thread , PullRequestWatchFailedError ) ;
222+ const target = yield * resolveTarget ( input , project ) ;
223+ const watchedLink = ( shell : OrchestrationV2ThreadShell ) =>
224+ threadPullRequestsOf ( shell ) . find (
225+ ( link ) => link . source !== "stack-dismissed" && threadPullRequestKeysEqual ( link , target ) ,
226+ ) ;
227+ const before = watchedLink ( thread ) ;
228+ const state = before ?. snapshot ?. state ;
229+ if ( watching && state !== undefined && state !== "open" ) {
230+ return yield * new PullRequestNotOpenError ( { state } ) ;
231+ }
232+ yield * engine
233+ . dispatch ( {
234+ type : "thread.pull-request.watch" ,
235+ commandId : yield * commandId ( "mcp-pr-watch" , thread . id ) ,
236+ threadId : thread . id ,
237+ host : target . host ,
238+ repository : target . repository ,
239+ number : target . number ,
240+ watching,
241+ ...( watching ? { link : { url : target . url , source : "agent" as const } } : { } ) ,
242+ } )
243+ . pipe ( Effect . catchCause ( dispatchFailure ( PullRequestWatchFailedError ) ) ) ;
244+ const after = yield * requireThread ( PullRequestWatchFailedError ) ;
245+ return {
246+ host : target . host ,
247+ repository : target . repository ,
248+ number : target . number ,
249+ url : target . url ,
250+ watching : watchedLink ( after ) ?. watch !== undefined ,
251+ wasWatching : before ?. watch !== undefined ,
252+ } ;
253+ } ) ;
254+
197255 return PullRequestsToolkit . of ( {
198256 link_pull_request : ( input ) =>
199257 Effect . gen ( function * ( ) {
@@ -260,6 +318,8 @@ const make = Effect.gen(function* () {
260318 } ) ,
261319 list_thread_pull_requests : ( ) =>
262320 requireThread ( PullRequestListFailedError ) . pipe ( Effect . map ( listThreadPullRequests ) ) ,
321+ watch_pull_request : ( input ) => setWatching ( input , true ) ,
322+ unwatch_pull_request : ( input ) => setWatching ( input , false ) ,
263323 } ) ;
264324} ) ;
265325
0 commit comments