Skip to content

Commit a16d42a

Browse files
authored
Firecrawl Tool (#481)
1 parent caeaf03 commit a16d42a

File tree

8 files changed

+174
-1
lines changed

8 files changed

+174
-1
lines changed

firecrawl/Makefile

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.PHONY: build
2+
build:
3+
go build -o bin/gptscript-go-tool .

firecrawl/cmd/scrape.go

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package cmd
2+
3+
import (
4+
"context"
5+
"encoding/json"
6+
"fmt"
7+
8+
"github.com/mendableai/firecrawl-go"
9+
)
10+
11+
func Scrape(ctx context.Context, apiKey, url string) (string, error) {
12+
// Parameters are already validated in main.go, so we can proceed directly
13+
14+
// Initialize the FirecrawlApp
15+
apiUrl := "https://api.firecrawl.dev"
16+
app, err := firecrawl.NewFirecrawlApp(apiKey, apiUrl)
17+
if err != nil {
18+
return "", fmt.Errorf("failed to initialize FirecrawlApp: %w", err)
19+
}
20+
21+
// Scrape with hardcoded format
22+
params := &firecrawl.ScrapeParams{
23+
Formats: []string{"markdown"},
24+
}
25+
26+
scrapeResult, err := app.ScrapeURL(url, params)
27+
if err != nil {
28+
return "", fmt.Errorf("failed to scrape URL: %w", err)
29+
}
30+
31+
resultJSON, err := json.MarshalIndent(scrapeResult, "", " ")
32+
if err != nil {
33+
return "", fmt.Errorf("failed to marshal scrape result: %w", err)
34+
}
35+
36+
return string(resultJSON), nil
37+
}

firecrawl/credential/tool.gpt

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
Name: Firecrawl Credential
2+
Description: Credentials for the Firecrawl API
3+
Share Credential: firecrawl-cred as firecrawl
4+
Type: credential
5+
6+
---
7+
Name: firecrawl-cred
8+
Tools: ../../generic-credential
9+
10+
#!sys.call ../../generic-credential
11+
12+
{
13+
"promptInfo": {
14+
"fields" : [
15+
{
16+
"name": "Firecrawl API key",
17+
"description": "Your Firecrawl API key",
18+
"env": "FIRECRAWL_API_KEY",
19+
"sensitive": true
20+
}
21+
],
22+
"message": "Enter your Firecrawl API key."
23+
}
24+
}
25+

firecrawl/go.mod

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module github.com/obot-platform/tools/firecrawl
2+
3+
go 1.23.4
4+
5+
require github.com/mendableai/firecrawl-go v1.0.0 // indirect

firecrawl/go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
github.com/mendableai/firecrawl-go v1.0.0 h1:nABWG1eaYtthPAwu8dmUNXz3DcSnV28EdvtHgA5ES+I=
2+
github.com/mendableai/firecrawl-go v1.0.0/go.mod h1:mTGbJ37fy43aaqonp/tdpzCH516jHFw/XVvfFi4QXHo=

firecrawl/main.go

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"os"
7+
"strings"
8+
9+
"github.com/obot-platform/tools/firecrawl/cmd"
10+
)
11+
12+
func validateAPIKey() string {
13+
apiKey := strings.TrimSpace(os.Getenv("FIRECRAWL_API_KEY"))
14+
if apiKey == "" {
15+
exitWithError("API key is required")
16+
}
17+
return apiKey
18+
}
19+
20+
func validateRequiredParam(value, name string) string {
21+
value = strings.TrimSpace(value)
22+
if value == "" {
23+
exitWithError(fmt.Sprintf("%s is required", name))
24+
}
25+
return value
26+
}
27+
28+
func exitWithError(msg string) {
29+
fmt.Println(msg)
30+
os.Exit(1)
31+
}
32+
33+
func main() {
34+
if len(os.Args) != 2 {
35+
fmt.Println("Usage: gptscript-go-tool <command>")
36+
os.Exit(1)
37+
}
38+
command := os.Args[1]
39+
40+
var (
41+
result string
42+
err error
43+
ctx = context.Background()
44+
)
45+
46+
apiKey := validateAPIKey()
47+
48+
switch command {
49+
case "scrapeUrl":
50+
url := validateRequiredParam(os.Getenv("URL"), "URL")
51+
result, err = cmd.Scrape(ctx, apiKey, url)
52+
53+
default:
54+
exitWithError(fmt.Sprintf("unknown command: %s", command))
55+
}
56+
57+
if err != nil {
58+
exitWithError(err.Error())
59+
}
60+
61+
fmt.Print(result)
62+
}

firecrawl/tool.gpt

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
---
2+
Name: Firecrawl
3+
Description: Tools for interacting with Firecrawl.
4+
Metadata: bundle: false
5+
Share Tools: Scrape URL
6+
7+
---
8+
Name: Scrape URL
9+
Description: Scrape a URL and convert it to markdown using Firecrawl.
10+
Share Context: Firecrawl Context
11+
Credentials: ./credential
12+
Param: url: The URL to scrape.
13+
14+
#!${GPTSCRIPT_TOOL_DIR}/bin/gptscript-go-tool scrapeUrl
15+
16+
---
17+
Name: Firecrawl Context
18+
Type: context
19+
20+
#!sys.echo
21+
22+
# START INSTRUCTIONS: Scrape URL tool
23+
24+
The Scrape URL tool allows you to scrape websites using Firecrawl.
25+
26+
When calling the Scrape URL tool:
27+
- The `url` parameter is required.
28+
- The tool will automatically convert the webpage content to markdown format.
29+
- Always validate the URL before scraping.
30+
- The tool returns full scrape results including markdown content and metadata.
31+
32+
# END INSTRUCTIONS: Scrape URL tool
33+
34+
---
35+
!metadata:*:icon
36+
https://firecrawl.dev/favicon.ico
37+

index.yaml

+3-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,9 @@ tools:
7575
reference: ./smartthings
7676
gemini-image-generator:
7777
reference: ./gemini/image-generator.gpt
78-
78+
firecrawl:
79+
reference: ./firecrawl
80+
7981
knowledgeDataSources:
8082
notion-data-source:
8183
reference: ./knowledge/data-sources/notion

0 commit comments

Comments
 (0)