-
Notifications
You must be signed in to change notification settings - Fork 95
feat(x2a): MCP server tools #2565
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -61,6 +61,7 @@ import { | |
| githubAuthApiRef, | ||
| gitlabAuthApiRef, | ||
| } from '@backstage/core-plugin-api'; | ||
| import { ConsentPage } from './components/oauth2/ConsentPage'; | ||
|
|
||
| const app = createApp({ | ||
| apis, | ||
|
|
@@ -152,6 +153,7 @@ const routes = ( | |
| <Route path="/settings" element={<UserSettingsPage />} /> | ||
| <Route path="/catalog-graph" element={<CatalogGraphPage />} /> | ||
| <Route path="/notifications" element={<NotificationsPage />} /> | ||
| <Route path="/oauth2/authorize/:sessionId" element={<ConsentPage />} /> | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will be difficult to distribute within RHDH. Easy with Backstage upstream deployments. |
||
| </FlatRoutes> | ||
| ); | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,249 @@ | ||
| /* | ||
| * Copyright Red Hat, Inc. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| import { useCallback, useEffect, useState } from 'react'; | ||
| import { useParams } from 'react-router-dom'; | ||
| import { | ||
| useApi, | ||
| configApiRef, | ||
| fetchApiRef, | ||
| discoveryApiRef, | ||
| } from '@backstage/core-plugin-api'; | ||
| import { | ||
| Page, | ||
| Header, | ||
| Content, | ||
| Progress, | ||
| ResponseErrorPanel, | ||
| } from '@backstage/core-components'; | ||
| import { | ||
| Card, | ||
| CardContent, | ||
| CardActions, | ||
| Button, | ||
| Typography, | ||
| Divider, | ||
| Box, | ||
| } from '@material-ui/core'; | ||
| import { makeStyles } from '@material-ui/core/styles'; | ||
|
|
||
| interface Session { | ||
| id: string; | ||
| clientName?: string; | ||
| clientId: string; | ||
| redirectUri: string; | ||
| scopes?: string[]; | ||
| } | ||
|
|
||
| type ConsentState = | ||
| | { status: 'loading' } | ||
| | { status: 'error'; error: string } | ||
| | { status: 'loaded'; session: Session } | ||
| | { status: 'submitting'; session: Session; action: 'approve' | 'reject' } | ||
| | { status: 'completed'; action: 'approve' | 'reject' }; | ||
|
|
||
| const useStyles = makeStyles(theme => ({ | ||
| card: { | ||
| maxWidth: 600, | ||
| margin: `${theme.spacing(4)}px auto`, | ||
| }, | ||
| appHeader: { | ||
| display: 'flex', | ||
| alignItems: 'center', | ||
| gap: theme.spacing(2), | ||
| marginBottom: theme.spacing(2), | ||
| }, | ||
| callbackUrl: { | ||
| fontFamily: 'monospace', | ||
| fontSize: '0.85rem', | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: this should be taken from |
||
| backgroundColor: theme.palette.background.default, | ||
| padding: theme.spacing(1), | ||
| borderRadius: theme.shape.borderRadius, | ||
| wordBreak: 'break-all', | ||
| marginTop: theme.spacing(1), | ||
| }, | ||
| actions: { | ||
| display: 'flex', | ||
| justifyContent: 'flex-end', | ||
| gap: theme.spacing(1), | ||
| padding: theme.spacing(2), | ||
| }, | ||
| })); | ||
|
|
||
| const getHeaderTitle = (state: ConsentState): string => { | ||
| if (state.status === 'completed' && state.action === 'approve') { | ||
| return 'Authorization Approved'; | ||
| } | ||
| if (state.status === 'completed') { | ||
| return 'Authorization Denied'; | ||
| } | ||
| return 'Authorization Request'; | ||
| }; | ||
|
|
||
| export const ConsentPage = () => { | ||
| const { sessionId } = useParams<{ sessionId: string }>(); | ||
| const classes = useStyles(); | ||
| const configApi = useApi(configApiRef); | ||
| const fetchApi = useApi(fetchApiRef); | ||
| const discoveryApi = useApi(discoveryApiRef); | ||
| const appTitle = configApi.getOptionalString('app.title') ?? 'Backstage'; | ||
|
|
||
| const [state, setState] = useState<ConsentState>({ status: 'loading' }); | ||
|
|
||
| useEffect(() => { | ||
| if (!sessionId) { | ||
| setState({ status: 'error', error: 'No session ID provided' }); | ||
| return undefined; | ||
| } | ||
|
|
||
| let cancelled = false; | ||
| (async () => { | ||
| try { | ||
| const baseUrl = await discoveryApi.getBaseUrl('auth'); | ||
| const response = await fetchApi.fetch( | ||
| `${baseUrl}/v1/sessions/${sessionId}`, | ||
| ); | ||
|
|
||
| if (cancelled) return; | ||
| if (!response.ok) { | ||
| const text = await response.text(); | ||
| setState({ status: 'error', error: text || response.statusText }); | ||
| return; | ||
| } | ||
|
|
||
| const session: Session = await response.json(); | ||
| setState({ status: 'loaded', session }); | ||
| } catch (e: unknown) { | ||
| if (cancelled) return; | ||
| const message = e instanceof Error ? e.message : String(e); | ||
|
Check warning on line 130 in workspaces/x2a/packages/app/src/components/oauth2/ConsentPage.tsx
|
||
| setState({ status: 'error', error: message }); | ||
| } | ||
| })(); | ||
|
|
||
| return () => { | ||
| cancelled = true; | ||
| }; | ||
| }, [sessionId, discoveryApi, fetchApi]); | ||
|
|
||
| const handleAction = useCallback( | ||
| async (action: 'approve' | 'reject') => { | ||
| if (state.status !== 'loaded') return; | ||
|
|
||
| setState({ status: 'submitting', session: state.session, action }); | ||
|
|
||
| try { | ||
| const baseUrl = await discoveryApi.getBaseUrl('auth'); | ||
| const response = await fetchApi.fetch( | ||
| `${baseUrl}/v1/sessions/${sessionId}/${action}`, | ||
| { method: 'POST', headers: { 'Content-Type': 'application/json' } }, | ||
| ); | ||
|
|
||
| if (!response.ok) { | ||
| const text = await response.text(); | ||
| setState({ status: 'loaded', session: state.session }); | ||
| throw new Error(text || response.statusText); | ||
| } | ||
|
|
||
| const result = await response.json(); | ||
| setState({ status: 'completed', action }); | ||
|
|
||
| if (result.redirectUrl) { | ||
| window.location.href = result.redirectUrl; | ||
|
Check warning on line 163 in workspaces/x2a/packages/app/src/components/oauth2/ConsentPage.tsx
|
||
| } | ||
| } catch (e: unknown) { | ||
| const message = e instanceof Error ? e.message : String(e); | ||
|
Check warning on line 166 in workspaces/x2a/packages/app/src/components/oauth2/ConsentPage.tsx
|
||
| setState({ status: 'error', error: message }); | ||
| } | ||
| }, | ||
| [state, sessionId, discoveryApi, fetchApi], | ||
| ); | ||
|
|
||
| const headerTitle = getHeaderTitle(state); | ||
|
|
||
| return ( | ||
| <Page themeId="tool"> | ||
| <Header title={headerTitle} /> | ||
| <Content> | ||
| {state.status === 'loading' && ( | ||
| <Box display="flex" justifyContent="center" height={300}> | ||
| <Progress /> | ||
| </Box> | ||
| )} | ||
|
|
||
| {state.status === 'error' && ( | ||
| <ResponseErrorPanel | ||
| title="Authorization Error" | ||
| error={new Error(state.error)} | ||
| /> | ||
| )} | ||
|
|
||
| {state.status === 'completed' && ( | ||
| <Typography variant="h5" align="center" style={{ marginTop: 32 }}> | ||
| {state.action === 'approve' | ||
| ? `You have successfully authorized the application to access your ${appTitle} account. Redirecting...` | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. All strings must be internationalized |
||
| : `You have denied the application access to your ${appTitle} account.`} | ||
| </Typography> | ||
| )} | ||
|
|
||
| {(state.status === 'loaded' || state.status === 'submitting') && ( | ||
| <Card className={classes.card}> | ||
| <CardContent> | ||
| <Box className={classes.appHeader}> | ||
| <Box> | ||
| <Typography variant="h6"> | ||
| {state.session.clientName ?? state.session.clientId} | ||
| </Typography> | ||
| <Typography variant="body2" color="textSecondary"> | ||
| wants to access your {appTitle} account | ||
| </Typography> | ||
| </Box> | ||
| </Box> | ||
| <Divider /> | ||
| <Box mt={2}> | ||
| <Typography variant="body2"> | ||
| This will grant the application a token to access {appTitle}{' '} | ||
| on your behalf. Only authorize applications you trust. | ||
| </Typography> | ||
| <Box className={classes.callbackUrl}> | ||
| {state.session.redirectUri} | ||
| </Box> | ||
| </Box> | ||
| </CardContent> | ||
| <CardActions className={classes.actions}> | ||
| <Button | ||
| variant="outlined" | ||
| color="secondary" | ||
| disabled={state.status === 'submitting'} | ||
| onClick={() => handleAction('reject')} | ||
| > | ||
| Cancel | ||
| </Button> | ||
| <Button | ||
| variant="contained" | ||
| color="primary" | ||
| disabled={state.status === 'submitting'} | ||
| onClick={() => handleAction('approve')} | ||
| > | ||
| {state.status === 'submitting' && state.action === 'approve' | ||
| ? 'Authorizing...' | ||
| : 'Authorize'} | ||
| </Button> | ||
| </CardActions> | ||
| </Card> | ||
| )} | ||
| </Content> | ||
| </Page> | ||
| ); | ||
| }; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should this be auth enabled?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, it is what allows the oauth flow in MCP
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oops sorry. I meant "Auto enabled". as in do we want this enabled by default