@mediagraph/mcp 1.0.1 → 1.0.2

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.
Files changed (2) hide show
  1. package/dist/index.js +92 -23
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -10,6 +10,8 @@ import {
10
10
  ListToolsRequestSchema,
11
11
  ReadResourceRequestSchema
12
12
  } from "@modelcontextprotocol/sdk/types.js";
13
+ import { exec } from "child_process";
14
+ import { platform } from "os";
13
15
 
14
16
  // src/auth/oauth.ts
15
17
  import { createHash, randomBytes } from "crypto";
@@ -2941,6 +2943,46 @@ var oauthHandler = new OAuthHandler({
2941
2943
  redirectPort: config.redirectPort
2942
2944
  });
2943
2945
  var currentTokens = null;
2946
+ var isAuthInProgress = false;
2947
+ function openBrowser(url) {
2948
+ const command = platform() === "darwin" ? `open "${url}"` : platform() === "win32" ? `start "" "${url}"` : `xdg-open "${url}"`;
2949
+ exec(command, (error) => {
2950
+ if (error) {
2951
+ console.error("Failed to open browser:", error);
2952
+ }
2953
+ });
2954
+ }
2955
+ async function runAutoAuth() {
2956
+ if (isAuthInProgress) {
2957
+ while (isAuthInProgress) {
2958
+ await new Promise((resolve) => setTimeout(resolve, 100));
2959
+ }
2960
+ return currentTokens !== null;
2961
+ }
2962
+ isAuthInProgress = true;
2963
+ try {
2964
+ const authUrl = oauthHandler.getAuthorizationUrl();
2965
+ openBrowser(authUrl);
2966
+ const { code } = await oauthHandler.waitForCallback();
2967
+ const tokens = await oauthHandler.exchangeCode(code);
2968
+ currentTokens = tokens;
2969
+ const whoami = await client.whoami();
2970
+ const storedData = {
2971
+ tokens,
2972
+ organizationId: whoami.organization.id,
2973
+ organizationName: whoami.organization.name,
2974
+ userId: whoami.user.id,
2975
+ userEmail: whoami.user.email
2976
+ };
2977
+ tokenStore.save(storedData);
2978
+ return true;
2979
+ } catch (error) {
2980
+ console.error("Auto-auth failed:", error);
2981
+ return false;
2982
+ } finally {
2983
+ isAuthInProgress = false;
2984
+ }
2985
+ }
2944
2986
  async function getAccessToken() {
2945
2987
  if (currentTokens && Date.now() < currentTokens.expires_at - 3e5) {
2946
2988
  return currentTokens.access_token;
@@ -2992,27 +3034,32 @@ server.setRequestHandler(ListToolsRequestSchema, async () => {
2992
3034
  });
2993
3035
  server.setRequestHandler(CallToolRequestSchema, async (request) => {
2994
3036
  const { name, arguments: args } = request.params;
2995
- const token = await getAccessToken();
3037
+ let token = await getAccessToken();
2996
3038
  if (!token) {
2997
- return {
2998
- content: [
2999
- {
3000
- type: "text",
3001
- text: `Not authenticated with Mediagraph. Please run the authorization flow first.
3002
-
3003
- To authorize:
3004
- 1. Run: npx @mediagraph/mcp authorize
3005
- 2. Complete the OAuth flow in your browser
3006
- 3. Return here and try again
3007
-
3008
- Or set your credentials via environment variables:
3009
- - MEDIAGRAPH_CLIENT_ID (required)
3010
- - MEDIAGRAPH_CLIENT_SECRET (optional, for confidential clients)
3011
- `
3012
- }
3013
- ],
3014
- isError: true
3015
- };
3039
+ const authSuccess = await runAutoAuth();
3040
+ if (!authSuccess) {
3041
+ return {
3042
+ content: [
3043
+ {
3044
+ type: "text",
3045
+ text: "Failed to authenticate with Mediagraph. Please try again or check your browser for the authorization window."
3046
+ }
3047
+ ],
3048
+ isError: true
3049
+ };
3050
+ }
3051
+ token = await getAccessToken();
3052
+ if (!token) {
3053
+ return {
3054
+ content: [
3055
+ {
3056
+ type: "text",
3057
+ text: "Authentication completed but failed to retrieve access token. Please try again."
3058
+ }
3059
+ ],
3060
+ isError: true
3061
+ };
3062
+ }
3016
3063
  }
3017
3064
  const result = await handleTool(name, args || {}, toolContext);
3018
3065
  return {
@@ -3026,7 +3073,14 @@ server.setRequestHandler(ListResourceTemplatesRequestSchema, async () => {
3026
3073
  };
3027
3074
  });
3028
3075
  server.setRequestHandler(ListResourcesRequestSchema, async () => {
3029
- const token = await getAccessToken();
3076
+ let token = await getAccessToken();
3077
+ if (!token) {
3078
+ const authSuccess = await runAutoAuth();
3079
+ if (!authSuccess) {
3080
+ return { resources: [] };
3081
+ }
3082
+ token = await getAccessToken();
3083
+ }
3030
3084
  if (!token) {
3031
3085
  return { resources: [] };
3032
3086
  }
@@ -3035,14 +3089,29 @@ server.setRequestHandler(ListResourcesRequestSchema, async () => {
3035
3089
  });
3036
3090
  server.setRequestHandler(ReadResourceRequestSchema, async (request) => {
3037
3091
  const { uri } = request.params;
3038
- const token = await getAccessToken();
3092
+ let token = await getAccessToken();
3093
+ if (!token) {
3094
+ const authSuccess = await runAutoAuth();
3095
+ if (!authSuccess) {
3096
+ return {
3097
+ contents: [
3098
+ {
3099
+ uri,
3100
+ mimeType: "text/plain",
3101
+ text: "Failed to authenticate with Mediagraph. Please try again."
3102
+ }
3103
+ ]
3104
+ };
3105
+ }
3106
+ token = await getAccessToken();
3107
+ }
3039
3108
  if (!token) {
3040
3109
  return {
3041
3110
  contents: [
3042
3111
  {
3043
3112
  uri,
3044
3113
  mimeType: "text/plain",
3045
- text: "Not authenticated with Mediagraph. Please authorize first."
3114
+ text: "Authentication completed but failed to retrieve access token."
3046
3115
  }
3047
3116
  ]
3048
3117
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mediagraph/mcp",
3
- "version": "1.0.1",
3
+ "version": "1.0.2",
4
4
  "description": "MCP server for Mediagraph - Media Asset Management Platform",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",